| 1 |
.. title:: clang-tidy - bugprone-compare-pointer-to-member-virtual-function |
| 2 |
|
| 3 |
bugprone-compare-pointer-to-member-virtual-function |
| 4 |
=================================================== |
| 5 |
|
| 6 |
Detects unspecified behavior about equality comparison between pointer to member |
| 7 |
virtual function and anything other than null-pointer-constant. |
| 8 |
|
| 9 |
.. code-block:: c++ |
| 10 |
|
| 11 |
struct A { |
| 12 |
void f1(); |
| 13 |
void f2(); |
| 14 |
virtual void f3(); |
| 15 |
virtual void f4(); |
| 16 |
|
| 17 |
void g1(int); |
| 18 |
}; |
| 19 |
|
| 20 |
void fn() { |
| 21 |
bool r1 = (&A::f1 == &A::f2); // ok |
| 22 |
bool r2 = (&A::f1 == &A::f3); // bugprone |
| 23 |
bool r3 = (&A::f1 != &A::f3); // bugprone |
| 24 |
bool r4 = (&A::f3 == nullptr); // ok |
| 25 |
bool r5 = (&A::f3 == &A::f4); // bugprone |
| 26 |
|
| 27 |
void (A::*v1)() = &A::f3; |
| 28 |
bool r6 = (v1 == &A::f1); // bugprone |
| 29 |
bool r6 = (v1 == nullptr); // ok |
| 30 |
|
| 31 |
void (A::*v2)() = &A::f2; |
| 32 |
bool r7 = (v2 == &A::f1); // false positive, but potential risk if assigning other value to v2. |
| 33 |
|
| 34 |
void (A::*v3)(int) = &A::g1; |
| 35 |
bool r8 = (v3 == &A::g1); // ok, no virtual function match void(A::*)(int) signature. |
| 36 |
} |
| 37 |
|
| 38 |
Provide warnings on equality comparisons involve pointers to member virtual |
| 39 |
function or variables which is potential pointer to member virtual function and |
| 40 |
any entity other than a null-pointer constant. |
| 41 |
|
| 42 |
In certain compilers, virtual function addresses are not conventional pointers |
| 43 |
but instead consist of offsets and indexes within a virtual function table |
| 44 |
(vtable). Consequently, these pointers may vary between base and derived |
| 45 |
classes, leading to unpredictable behavior when compared directly. This issue |
| 46 |
becomes particularly challenging when dealing with pointers to pure virtual |
| 47 |
functions, as they may not even have a valid address, further complicating |
| 48 |
comparisons. |
| 49 |
|
| 50 |
Instead, it is recommended to utilize the ``typeid`` operator or other appropriate |
| 51 |
mechanisms for comparing objects to ensure robust and predictable behavior in |
| 52 |
your codebase. By heeding this detection and adopting a more reliable comparison |
| 53 |
method, you can mitigate potential issues related to unspecified behavior, |
| 54 |
especially when dealing with pointers to member virtual functions or pure |
| 55 |
virtual functions, thereby improving the overall stability and maintainability |
| 56 |
of your code. In scenarios involving pointers to member virtual functions, it's |
| 57 |
only advisable to employ ``nullptr`` for comparisons. |
| 58 |
|
| 59 |
Limitations |
| 60 |
----------- |
| 61 |
|
| 62 |
Does not analyze values stored in a variable. For variable, only analyze all virtual |
| 63 |
methods in the same ``class`` or ``struct`` and diagnose when assigning a pointer |
| 64 |
to member virtual function to this variable is possible. |