Rule A5-3-2 (required, implementation, partially automated)

Null pointers shall not be dereferenced.

Rationale

Dereferencing a NULL pointer leads to undefined behavior. Note: It is required requires that a pointer is checked for non-NULL status before dereferencing occurs.

Example

// $Id: A5-3-2.cpp 305629 2018-01-29 13:29:25Z piotr.serwa $
#include <iostream>
#include <memory>
#include <cstdint>

class A
{
public:
A(std::uint32_t a) : a(a) {}
std::uint32_t GetA() const noexcept { return a; }

private:
std::uint32_t a;
};

bool Sum(const A* lhs, const A* rhs)

{
//non-compliant, not checking if pointer is invalid
return lhs->GetA() + rhs->GetA();

}

int main(void)
{
auto l = std::make_shared<A>(3);
decltype(l) r;

auto sum = Sum(l.get(), r.get());

std::cout << sum << std::endl;
return 0;

}

See also

JSF December 2005 [8]: AV Rule 174: The null pointer shall not be dereferenced. SEI CERT C++ Coding Standard [10]: EXP34-C: Do not dereference null pointers. C++ Core Guidelines [11]: ES.65: Don’t dereference an invalid pointer.