AutosarC++19_03-A8.5.0

All memory shall be initialized before it is read

Required inputs: IR

Rationale

Objects with automatic or dynamic storage duration are default-initialized if no initializer is specified. Default initialization produces indeterminate values for objects of neither class nor array types. Default initialization of array types leads to default initialization of each array element. Reading from indeterminate values may produce undefined behavior.

Thus, all local variables, member variables, or objects allocated dynamically must be explicitly initialized before their values are read, unless they are of class type or array of non-fundamental type. It is recommended practice to initialize all such objects immediately when they are defined.

Note: Zero-initialization will happen before any other initialization for any objects with static or thread-local storage duration. Thus, such objects need not be explicitly initialized.

Example
// $Id: A8-5-0.cpp 307536 2018-02-14 12:35:11Z jan.babst $
    #include <cstdint>
    #include <string>

    static std::int32_t zero;     // Compliant - Variable with static storage duration
                                  // is zero-initialized.

    void local()
    {
        std::int32_t a;      // No initialization
        std::int32_t b{};    // Compliant - zero initialization

        b = a;       // Non-compliant - uninitialized memory read
        a = zero;    // Compliant - a is zero now
        b = a;       // Compliant - read from initialized memory

        std::string s;   // Compliant - default constructor is a called
        // read from s
    }

    void dynamic()
    {
        // Note: These examples violate A18-5-2

        auto const a = new std::int32_t;     // No initialization
        auto const b = new std::int32_t{};   // Compliant - zero initialization

        *b = *a;     // Non-compliant - uninitialized memory read
        *a = zero;   // Compliant - a is zero now
        *b = *a;     // Compliant - read from initialized memory

        delete b;
        delete a;

        auto const s =
            new std::string;   // Compliant - default constructor is a called
        // read from *s
        delete s;
    }

    // Members of Bad are default-initialized by the (implicitly generated) default
    // constructor. Note that this violates A12-1-1.
    struct Bad
    {
        std::int32_t a;
        std::int32_t b;
    };

    // Compliant - Members of Good are explicitly initialized.
    // This also complies to A12-1-1.
    struct Good
    {
        std::int32_t a{0};
        std::int32_t b{0};
    };

    void members()
    {
        Bad bad; // Default constructor is called, but members a not initialized

        bad.b = bad.a;   // Non-compliant - uninitialized memory read
        bad.a = zero;    // Compliant - bad.a is zero now
        bad.b = bad.a;   // Compliant - read from initialized memory

        Good good;   // Default constructor is called and initializes members

        std::int32_t x = good.a;   // Compliant
          std::int32_t y = good.b;     // Compliant
    }
See Also
  • MISRA C++ 2008 [7]: 8-5-1: All variables shall have a defined value before they are used.
  • HIC++ v4.0 [9]: 8.4.1: Do not access an invalid object or an object with indeterminate value
  • JSF December 2005 [8]: AV Rule 142: All variables shall be initialized before use.
  • SEI CERT C++ Coding Standard [10]: EXP53-CPP: Do not read uninitialized memory
  • C++ Core Guidelines [11]: ES.20: Always initialize an object
  • ISO/IEC 14882:2014 [3]: 8.5: [dcl.init]

Possible Messages

Key

Text

Severity

Disabled

assigned_to_pointer_to_const

Assigning the address of a partially initialized variable to some pointer-to-const

None

False

pass_as_pointer_to_const_param

Passing uninitialized variable by pointer as function parameter with pointer-to-const type

None

False

possible_return_value_uninit

Function return value is potentially not initialized

None

False

possible_uninit

Use of possibly uninitialized variable

None

False

possibly_initialized

Use of possibly uninitialized variable (previous call {node0} might have initialized the variable)

None

False

return_value_uninit

Function return value is not initialized

None

False

uninit

Use of uninitialized variable

None

False

Options

additional_local_array_check

additional_local_array_check : bool = True

Invoke an additional analysis that tries to remove false positives involving accesses to local array variables and in particular their initialization. The analysis attempts to report only the first use of an uninitialized value. Consider e.g. the following example:
    int example()
    {
        int a[10];
        int b[20];
        int uninit_var;
        for (int i = 0; i < 10; ++i)
        {
L1:         a[i] = uninit_var; // use of uninit_var reported
            b[i] = i;
        }
        int result = a[3]; // not reported, since already reported at L1
        result += b[15]; // reported; c[] is not (completely) initialized
        return result;
    }
    
 

assume_globals_are_initialized

assume_globals_are_initialized : bool = True

Whether global and local static variables should be treated as initialized (as specified by the language).
 

check_array_access_with_unknown_index

check_array_access_with_unknown_index : bool = False

Whether array accesses like a[i] with non-literal index i should be checked as well.
 

exclude_from_pointer_to_const_param_check

exclude_from_pointer_to_const_param_check : set[bauhaus.analysis.config.QualifiedName] = {'__builtin_object_size'}

Names of routines whose parameters should be excluded from the check for passing uninitialized variables by pointer as parameter with pointer-to-const type.
 

track_conditional_initialization

track_conditional_initialization : bool = True

Whether higher precision should be used to eliminate cases where the initialization and the access are controlled by conditions in a way that the variable access is only executed when the initialization was executed. Requires more memory and runtime but can eliminate some false positives.
 

use_semantic_analysis

use_semantic_analysis : bool = True

When enabled, use semantic analysis. Otherwise filter uninitialized variable messages from the compiler.
 

writing_into_pointer_to_const

writing_into_pointer_to_const

Type: dict[bauhaus.analysis.config.QualifiedName, int]

Default:

{
   'cudaMemcpyToSymbol': 0
}
Names of routines (mapping to parameter index, starting at 0) having a parameter declared as pointer-to-const yet they are still writing into the pointee.