AutosarC++18_10-A5.3.2¶
Null pointers shall not be dereferenced
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | partially automated |
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¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
allow_non_null_comparison¶
allow_non_null_comparison : bool = True
dereference_filter¶
dereference_filter : typing.Callable[[Direct_Object_Selection_Interface], bool] | None = None
ignored_object_types¶
ignored_object_types : set[bauhaus.ir.LIR_Class_Name] = set()
null_check_macro¶
null_check_macro : bauhaus.analysis.config.MacroName = ''
NULL.
null_check_routines¶
null_check_routines : set[bauhaus.analysis.config.QualifiedName] = set()
routines_returning_valid_pointers¶
routines_returning_valid_pointers : set[bauhaus.analysis.config.QualifiedName] = {'std::make_shared', 'std::make_unique'}
types_ignored_by_qualified_name¶
types_ignored_by_qualified_name : set[bauhaus.analysis.config.QualifiedName] = set()