JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
C and C++


CONCURRENCY.DATARACE : Data Race

Summary

Multiple execution threads access a shared piece of data, and at least one of them changes the value of that data, without an explicit synchronization operation to separate the accesses.

CodeSonar infers proper synchronization primarily based on the assumption that accesses to shared memory occur in critical sections defined by the acquisition of mutex locks. In applications that use other synchronization patterns (such as producer/consumer), CodeSonar is likely to issue many false positive Data Race warnings.

Properties

Class Name Data Race
Significance reliability
Mnemonic CONCURRENCY.DATARACE
Categories
MisraC2025 MisraC2025:D.5.1 There shall be no data races between threads
MisraC2023 MisraC2023:D.5.1 There shall be no data races between threads
Misra2012 Misra2012:D.5.1 There shall be no data races between threads
MisraC++2023 MisraC++2023:0.3.2 A function call shall not violate the function's preconditions
CWE CWE:362 Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition')
  CWE:364 Signal Handler Race Condition
  CWE:366 Race Condition within a Thread
  CWE:413 Improper Resource Locking
  CWE:567 Unsynchronized Access to Shared Data in a Multithreaded Context
CERT-C CERT-C:CON07-C Ensure that compound operations on shared variables are atomic
  CERT-C:CON32-C Prevent data races when accessing bit-fields from multiple threads
  CERT-C:CON43-C Do not allow data races in multithreaded code
  CERT-C:POS49-C When data must be accessed by multiple threads, provide a mutex and guarantee no adjacent data is also accessed
  CERT-C:SIG31-C Do not access shared objects in signal handlers
CERT-CPP CERT-CPP:CON52-CPP Prevent data races when accessing bit-fields from multiple threads
DISA-6r1 DISA-6r1:V-222567 The application must not be vulnerable to race conditions.
DISA-5r3 DISA-5r3:V-70185 The application must not be vulnerable to race conditions.
DISA-4r3 DISA-4r3:V-70185 The application must not be vulnerable to race conditions.
DISA-3r10 DISA-3r10:V-16815 The designer will ensure the application is not vulnerable to race conditions.
OWASP-2021 OWASP-2021:A4 Insecure design
OWASP-2025 OWASP-2025:A06 Insecure Design
Availability Available for C and C++.
Enabling Checks for this warning class are disabled by default. To enable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += allow class="Data Race"
We also recommend that you increase the values of TIME_LIMIT_INTER_CLASSIFY and TIME_LIMIT_INTRA_CLASSIFY. In both cases, a good general rule is to double the value that was used when Data Race was disabled.

Thread Entry Points

The Data Race analysis will treat a function f() as a thread entry point if f() is passed to any of the following, as described on the Concurrency Models: Thread Entry Points page.

[For programs using object-oriented multithreading APIs only.] The Data Race analysis will treat an object method o.m() as a thread entry point if o is used in a location where a thead entry point is expected and m() matches a THREAD_ENTRY_METHOD_NAMES configuration file rule.

Example

#include <pthread.h>

int count;

void update_count(void){
    int tmp = count;                                             /* read without locking */
    tmp = tmp+1;
    count = tmp;       /* 'Data Race' warning issued here */     /* write without locking */
}

void *watch_Y(void *arg){
    while (1){
        /* If see a yellow object, then... */
        update_count();
        /* Do yellow-specific tasks. */
    }
}

void *watch_R(void *arg){
    while (1){
        /* If see a red object, then... */
        update_count();
        /* Do red-specific tasks. */
    }
}

int main(void) {
    pthread_t Y_pth, R_pth;
    int y_res, r_res = 0;
    int retval = 0;
    y_res = pthread_create(&Y_pth, NULL, watch_Y, "yellow");
    r_res = pthread_create(&R_pth, NULL, watch_R, "red");
    /* ... */
    if (!y_res)
        pthread_detach(Y_pth);
    if (!r_res)
        pthread_detach(R_pth);
    return y_res + r_res;
}

In a multithreaded environment, data races can occur in the body of update_count():

Data Race example diagram

Note

Even if the Data Race warning class is enabled, CodeSonar will only perform checking for this class if there is at least one location in the project that invokes one the following threading primitives.

Note in particular that identifying a function with configuration parameter FORCE_THREAD_ENTRY_NAMES or THREAD_ENTRY_METHOD_NAMES is not sufficient to ensure that Data Race checking will be performed.

Relevant Configuration File Parameters

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

 

To report problems with this documentation, please visit https://support.codesecure.com/.