Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-3687

Atomic types should be used instead of "volatile" types

    XMLWordPrintable

    Details

    • Message:
      Convert this "volatile" type into an atomic type.
    • Highlighting:
      Hide

      volatile keyword

      Show
      volatile keyword
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      C, C++, Objective-C
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      1h
    • Analysis Scope:
      Main Sources, Test Sources
    • CppCoreGuidelines:
      CP.200

      Description

      The main intended use-case for volatile in C and C++ is to access data that can be modified by something external to the program, typically some hardware register. In contrast with other languages that provide a volatile keyword, it does not provide any useful guarantees related to atomicity, memory ordering, or inter-thread synchronization. It is only really needed for the kind of low-level code found in kernels or embedded software, i.e. using memory-mapped I/O registers to manipulate hardware directly.

      According to the C standard:

      volatile is a hint to the implementation to avoid aggressive optimization involving the object because the value of the object might be changed by means undetectable by an implementation.

      Only C11/C++11 "atomic types" are free from data races, and you should use them or synchronization primitives if you want to avoid race conditions.

      This rule raises an issue when a local variable or class data member is declared as volatile (at the top level of the type, pointers to volatile are not reported).

      Noncompliant Code Example

      volatile int counter; // Noncompliant
      User * volatile vpUser; // Noncompliant; pointer is volatile 
      User volatile * pvUser;  // Compliant; User instance is volatile, not the pointer
      

      Compliant Solution

      atomic_int counter;
      std::atomic<User*> vpUser;
      User volatile * pvUser;
      

      See

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              alban.auzeill Alban Auzeill
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: