C and C++


CONCURRENCY.C_ATOMIC.INIT : Inappropriate C Atomic Initialization

Summary

An atomic variable is accessed, but it has not been initialized. There are two cases:

For the sake of this check, a variable is considered to be atomic if it is declared with the _Atomic type specifier or has any atomic type defined in <stdatomic.h>.

Note: if an atomic variable is used without being initialized, CodeSonar will issue an Uninitialized Variable warning.

Properties

Class Name Inappropriate C Atomic Initialization
Significance reliability
Mnemonic CONCURRENCY.C_ATOMIC.INIT
Categories
MisraC2023 MisraC2023:1.3 There shall be no occurrence of undefined or critical unspecified behaviour
  MisraC2023:9.7 Atomic objects shall be appropriately initialized before being accessed
Misra2012 Misra2012:1.3 There shall be no occurrence of undefined or critical unspecified behaviour
  Misra2012:9.7 Atomic objects shall be appropriately initialized before being accessed
CWE CWE:665 Improper Initialization
  CWE:908 Use of Uninitialized Resource
Availability Available for C and C++.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Inappropriate C Atomic Initialization"

Example

#include <stdint.h>
#include <stdatomic.h>

int32_t concurrency_c_atomic_init( void ){
    _Atomic int32_t good1 = 111;                               /* ok: direct initialization */
    _Atomic int32_t good2;
    int32_t _Atomic * goodp;
    _Atomic int32_t bad1;
    _Atomic int32_t bad2;
    _Atomic int32_t bad3;
    int32_t _Atomic * badp;

    atomic_init(&good2, 222);                                 /* ok: initialization with atomic_init() */

    good1 = 555;                                              /* ok: good1 previously initialized */
    goodp = &good2;                                           /* ok: good2 previously initialized */

    bad1 = 333;                       /* 'Inappropriate C Atomic Initialization' warning issued here
                                       *  - not direct initialization and not using atomic_init()
                                       */
    badp = &bad2;                     /* 'Inappropriate C Atomic Initialization' warning issued here
                                       * - taking address of uninitialized atomic variable
                                       */
    *badp = 444;                      /* 'Inappropriate C Atomic Initialization' warning issued here
                                       * - writing through pointer into uninitialized atomic variable
                                       */

    return bad3;                               /* 'Uninitialized Variable' warning issued here
                                                * - use of ininitialized atomic variable
                                                */
}

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.