AutosarC++18_03-A5.5.1

A pointer to member shall not access non-existent class members

Required inputs: IR, StaticSemanticAnalysis

Rationale

Usage of a pointer-to-member expression leads to undefined behavior in the following cases:

  • The pointer to member is a null pointer.
  • The dynamic type of the object does not contain the member to which the called pointer to member refers.
Example
// $Id: A5-5-1.cpp 302200 2017-12-20 17:17:08Z michal.szczepankiewicz $

class A {
public:
  virtual ~A() = default;
};

class AA : public A {
public:
  virtual ~AA() = default;
  virtual void foo() {}

  using ptr = void (AA::*)();
};

class B {
public:
  static AA::ptr foo_ptr2;
};

AA::ptr B::foo_ptr2;

int main(void) {
  A *a = new A();
  void (A::*foo_ptr1)() = static_cast<void (A::*)()>(&AA::foo);
  (a->*foo_ptr1)(); // non-compliant
  delete a;

  AA *aa = new AA();
  (aa->*B::foo_ptr2)(); // non-compliant, not explicitly initialized
  delete aa;

  return 0;
}
See Also
  • SEI CERT C++ Coding Standard [9]: OOP55-CPP: Do not use pointer-to- member operators to access nonexistent members

Possible Messages

Key

Text

Severity

Disabled

non-existing

Calling a non-existing member via a pointer-to-member call.

None

False

uninitialized

Uninitialized pointer-to-member called.

None

False

Options

pointer_target_limit

pointer_target_limit : int | None = 5

Maximal number of pointer targets per operand. If an operand refers to a larger number of pointer values it will not be considered by this rule. Lower values speed up the analysis but might cause false negatives. If unset, all targets are checked.