Static variable identified as non-const global variable

  • What language is this for? C++
  • Which rule? S5421
  • Why do you believe it’s a false-positive/false-negative? A static variable isn’t a global, it can’t be accessed from other files.
  • Are you using
    • SonarQube - Version 9.6.1 (build 59531)
    • SonarLint - VSCode v4.3.0
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
static bool verbose = false;

Hi @Tom_Isaacson2,

Indeed as you have pointed out, static variables at global scope have internal linkage and thus are not visible in other translation units (TUs)/source files. However, they are still in global scope in TU, can be accessed and modified by any function in the same file, and cause some issues as described in rule description - calling functions that access it from different threads may cause data race, and the same invocations of such functions may produce a different result. Note that, even if such an internal linkage variable is not visible in other source files, functions that refer to/modify it may be.

Given the above, we believe that an issue should be raised for non-const global static variables and this is true positive (TP).

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.