AutosarC++18_10-A5.3.2

Null pointers shall not be dereferenced

Required inputs: IR

Rationale

Dereferencing a NULL pointer leads to undefined behavior.

Note: It is required requires that a pointer is checked for non-NULL status before de- referencing 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 de- referenced.
  • 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.

Possible Messages

Key

Text

Severity

Disabled

unchecked_dereference

Unchecked dereference.

None

False

Options

allow_non_null_comparison

allow_non_null_comparison : bool = True

Whether to allow comparisons to non-null objects (e.g., &x for a given variable x) as checks.
 

dereference_filter

dereference_filter : typing.Callable[[Direct_Object_Selection_Interface], bool] | None = None

Filter predicate for dereferenced objects.
 

ignored_object_types

ignored_object_types : set[bauhaus.ir.LIR_Class_Name] = set()

Types of LIR classes which are allowed to be dereferenced without further checks.
 

null_check_macro

null_check_macro : bauhaus.analysis.config.MacroName = ''

Name of macro used to represent check for NULL.
 

null_check_routines

null_check_routines : set[bauhaus.analysis.config.QualifiedName] = set()

Qualified names of functions used to realize a null check.
 

routines_returning_valid_pointers

routines_returning_valid_pointers : set[bauhaus.analysis.config.QualifiedName] = {'std::make_shared', 'std::make_unique'}

Routines that always return valid pointers. No check is enforced for variables defined using the return value of one of the routines.
 

types_ignored_by_qualified_name

types_ignored_by_qualified_name : set[bauhaus.analysis.config.QualifiedName] = set()

Fully qualified names of types to be considered as non-null without further check.