53 lines · plain
1.. title:: clang-tidy - bugprone-multi-level-implicit-pointer-conversion2 3bugprone-multi-level-implicit-pointer-conversion4================================================5 6Detects implicit conversions between pointers of different levels of7indirection.8 9Conversions between pointer types of different levels of indirection can be10dangerous and may lead to undefined behavior, particularly if the converted11pointer is later cast to a type with a different level of indirection.12For example, converting a pointer to a pointer to an ``int`` (``int**``) to13a ``void*`` can result in the loss of information about the original level of14indirection, which can cause problems when attempting to use the converted15pointer. If the converted pointer is later cast to a type with a different16level of indirection and dereferenced, it may lead to access violations,17memory corruption, or other undefined behavior.18 19Consider the following example:20 21.. code-block:: c++22 23 void foo(void* ptr);24 25 int main() {26 int x = 42;27 int* ptr = &x;28 int** ptr_ptr = &ptr;29 foo(ptr_ptr); // warning will trigger here30 return 0;31 }32 33In this example, ``foo()`` is called with ``ptr_ptr`` as its argument. However,34``ptr_ptr`` is a ``int**`` pointer, while ``foo()`` expects a ``void*`` pointer.35This results in an implicit pointer level conversion, which could cause issues36if ``foo()`` dereferences the pointer assuming it's a ``int*`` pointer.37 38Using an explicit cast is a recommended solution to prevent issues caused by39implicit pointer level conversion, as it allows the developer to explicitly40state their intention and show their reasoning for the type conversion.41Additionally, it is recommended that developers thoroughly check and verify the42safety of the conversion before using an explicit cast. This extra level of43caution can help catch potential issues early on in the development process,44improving the overall reliability and maintainability of the code.45 46Options47-------48 49.. option:: EnableInC50 51 If `true`, enables the check in C code (it is always enabled in C++ code).52 Default is `true`.53