SonarSource Rules
  • Products

    In-IDE

    Code Quality and Security in your IDE with SonarQube Ide

    IDE extension that lets you fix coding issues before they exist!

    Discover SonarQube for IDE

    SaaS

    Code Quality and Security in the cloud with SonarQube Cloud

    Setup is effortless and analysis is automatic for most languages

    Discover SonarQube Cloud

    Self-Hosted

    Code Quality and Security Self-Hosted with SonarQube Server

    Fast, accurate analysis; enterprise scalability

    Discover SonarQube Server
  • SecretsSecrets
  • ABAPABAP
  • AnsibleAnsible
  • ApexApex
  • AzureResourceManagerAzureResourceManager
  • CC
  • C#C#
  • C++C++
  • CloudFormationCloudFormation
  • COBOLCOBOL
  • CSSCSS
  • DartDart
  • DockerDocker
  • FlexFlex
  • GitHub ActionsGitHub Actions
  • GoGo
  • HTMLHTML
  • JavaJava
  • JavaScriptJavaScript
  • JSONJSON
  • JCLJCL
  • KotlinKotlin
  • KubernetesKubernetes
  • Objective CObjective C
  • PHPPHP
  • PL/IPL/I
  • PL/SQLPL/SQL
  • PythonPython
  • RPGRPG
  • RubyRuby
  • RustRust
  • ScalaScala
  • ShellShell
  • SwiftSwift
  • TerraformTerraform
  • TextText
  • TypeScriptTypeScript
  • T-SQLT-SQL
  • VB.NETVB.NET
  • VB6VB6
  • XMLXML
  • YAMLYAML
C++

C++ static code analysis

Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your C++ code

  • All rules 674
  • Vulnerability13
  • Bug139
  • Security Hotspot19
  • Code Smell503

  • Quick Fix 91
Filtered: 118 rules found
based-on-misra
    Impact
      Clean code attribute
        1. "goto" should jump to labels declared later in the same function

           Code Smell
        2. The global namespace should only contain "main", namespace declarations, and "extern" C declarations

           Code Smell
        3. "<time.h>" should not be used

           Code Smell
        4. "atof", "atoi" and "atol" from <stdlib.h> should not be used

           Bug
        5. "<stdio.h>" should not be used in production code

           Code Smell
        6. "<signal.h>" should not be used

           Bug
        7. "errno" should not be used

           Code Smell
        8. Dynamic heap memory allocation should not be used

           Bug
        9. "setjmp" and "longjmp" should not be used

           Code Smell
        10. Reserved identifiers should not be defined or declared

           Code Smell
        11. Lines starting with "#" should contain valid preprocessing directives

           Bug
        12. Only standard forms of the "defined" directive should be used

           Code Smell
        13. Preprocessor operators "#" and "##" should not be used

           Code Smell
        14. Macros used in preprocessor directives should be defined before use

           Bug
        15. Function-like macros should not be invoked without all of their arguments

           Bug
        16. Function-like macros should not be used

           Code Smell
        17. "#undef" should be used with caution

           Code Smell
        18. Macros should not be #define'd or #undef'd within a block

           Code Smell
        19. "#include" directives should be followed by either <filename> or "filename" sequences

           Bug
        20. Non-standard characters should not occur in header file names in "#include" directives

           Bug
        21. Unions should not be used

           Code Smell
        22. The address of an automatic object should not be assigned to another object that may persist after the first object has ceased to exist

           Bug
        23. Array type function arguments should not decay to pointers

           Code Smell
        24. Object declarations should contain no more than 2 levels of pointer indirection

           Code Smell
        25. Function names should be used either as a call with a parameter list or with the "&" operator

           Code Smell
        26. Function exit paths should have appropriate return values

           Bug
        27. Parameters in a function prototype should be named

           Code Smell
        28. Recursion should not be used

           Code Smell
        29. Loops should not have more than one "break" or "goto" statement

           Code Smell
        30. The right-hand operands of && and || should not contain side effects

           Code Smell
        31. "continue" should not be used

           Code Smell
        32. "goto" statement should not be used

           Code Smell
        33. Non-empty statements should change control flow or have at least one side-effect

           Bug
        34. Unused type declarations should be removed

           Code Smell
        35. The three expressions of a "for" statement should only be concerned with loop control

           Code Smell
        36. Increment (++) and decrement (--) operators should not be used in a method call or mixed with other operators in an expression

           Code Smell
        37. Comma operator should not be used

           Code Smell
        38. Unary minus should not be applied to an unsigned expression

           Bug
        39. Bitwise operators should not be applied to signed operands

           Bug
        40. "enum" values should not be used as operands to built-in operators other than [ ], =, ==, !=, unary &, and the relational operators <, <=, >, >=

           Code Smell
        41. "bool" expressions should not be used as operands to built-in operators other than =, &&, ||, !, ==, !=, unary &, and the conditional operator

           Code Smell
        42. C-style and functional notation casts should not be used

           Code Smell
        43. Operands of "&&" and "||" should be primary (C) or postfix (C++) expressions

           Code Smell
        44. Boolean operations should not have numeric operands, and vice versa

           Bug
        45. Objects with integer type should not be converted to objects with pointer type

           Bug
        46. Pointer conversions should be restricted to a safe subset

           Bug
        47. Function pointers should not be converted to any other type

           Bug
        48. Constants of unsigned type should have a "U" suffix

           Code Smell
        49. Results of ~ and << operations on operands of underlying types unsigned char and unsigned short should immediately be cast to the operand's underlying type

           Bug
        50. Signed and unsigned types should not be mixed in expressions

           Code Smell
        51. "enum" members with implicit values should not collide with other enumerators

           Code Smell
        52. Variables should be initialized before use

           Bug
        53. Braces should be used to indicate and match the structure in the non-zero initialization of arrays and structures

           Code Smell
        54. Array declarations should include an explicit size specification

           Code Smell
        55. Literal suffix "L" for long integers shall be upper case

           Code Smell
        56. String literals with different prefixes should not be concatenated

           Bug
        57. Bit fields should be declared with appropriate types

           Code Smell
        58. typedefs that indicate size and signedness should be used in place of the basic types

           Code Smell
        59. Appropriate char types should be used for character and integer values

           Code Smell
        60. Identifiers should not be longer than 31 characters

           Code Smell
        61. Digraphs should not be used

           Code Smell
        62. Trigraphs should not be used

           Code Smell
        63. Only escape sequences defined in the ISO C standard should be used

           Bug
        64. All uses of the #pragma directive should be documented

           Code Smell
        65. Source code should only use /* ... */ style comments

           Code Smell
        66. Macro arguments should not contain preprocessing directives

           Bug
        67. Names of well-known C standard library macros and functions should not be used as identifiers

           Code Smell
        68. Relational and subtraction operators should not be used with pointers to different arrays

           Bug
        69. Pointer and reference local variables should be "const" if the corresponding object is not modified

           Code Smell
        70. Unevaluated operands should not have side effects

           Code Smell
        71. User-defined types should not be passed as variadic arguments

           Bug
        72. Keywords shall not be used as macros identifiers

           Code Smell
        73. "void *" should not be used in typedefs, member variables, function parameters or return type

           Code Smell
        74. "default" clauses should be first or last

           Code Smell
        75. Integral operations should not overflow

           Bug
        76. Non-exception types should not be thrown

           Code Smell
        77. Member variables should not be "protected"

           Code Smell
        78. Conditionally executed code should be reachable

           Bug
        79. Octal and hexadecimal escape sequences should be terminated

           Code Smell
        80. Flexible array members should not be declared

           Code Smell
        81. Line-splicing should not be used in "//" comments

           Bug
        82. Single-bit named bit fields should not be of a signed type

           Bug
        83. "for" loop counters should not have essentially floating type

           Bug
        84. "goto" statements should not be used to jump into blocks

           Code Smell
        85. Pointers should not be cast to integral types

           Bug
        86. All code should be reachable

           Bug
        87. Pre-defined macros should not be defined, redefined or undefined

           Code Smell
        88. "explicit" should be used on single-parameter constructors and conversion operators

           Code Smell
        89. Multiple variables should not be declared on the same line

           Code Smell
        90. Octal values should not be used

           Code Smell
        91. "switch" statements should have "default" clauses

           Code Smell
        92. "if" statements should be preferred over "switch" when simpler

           Code Smell
        93. Switch cases should end with an unconditional "break" statement

           Code Smell
        94. "for" loop stop conditions should be invariant

           Code Smell
        95. "operator delete" should be written along with "operator new"

           Bug
        96. "if ... else if" constructs should end with "else" clauses

           Code Smell
        97. Sections of code should not be commented out

           Code Smell
        98. Floating point numbers should not be tested for equality

           Bug
        99. "switch" statements should not contain non-case labels

           Code Smell
        100. Control structures should use curly braces

           Code Smell
        101. Unused function parameters should be removed

           Code Smell
        102. Unused functions and methods should be removed

           Code Smell
        103. Assignments should not be made from within conditions

           Code Smell
        104. Variables should not be shadowed

           Code Smell
        105. Empty statements should be removed

           Code Smell
        106. "/*" and "//" should not be used within comments

           Code Smell
        107. Unused labels should be removed

           Code Smell
        108. "<cstdio>" should not be used

           Code Smell
        109. "<ctime>" should not be used

           Code Smell
        110. Empty throws ("throw;") should only be used in the compound statements of catch handlers

           Bug
        111. Function templates should not be specialized

           Code Smell
        112. Pure "virtual" functions should not override non-pure "virtual" functions

           Code Smell
        113. Virtual functions should be declared with the "virtual" keyword

           Code Smell
        114. Accessible base classes should not be both "virtual" and non-virtual in the same hierarchy

           Bug
        115. Classes should not be derived from virtual bases

           Code Smell
        116. A function should have a single point of exit at the end of the function

           Code Smell
        117. "using namespace" directives should not be used in header files

           Code Smell
        118. "using-directives" should not be used

           Code Smell

        Variables should be initialized before use

        intentionality - logical
        reliability
        Bug
        • cwe
        • symbolic-execution
        • based-on-misra

        Variables should be initialized before their use to avoid unexpected behavior due to garbage values.

        Why is this an issue?

        How can I fix it?

        More Info

        A local variable of any built-in type (such as int, float, and pointers), declared without an initial value is not initialized to any particular value. Consequently, if no value is assigned to such a variable first, the code that uses it has no defined behavior.

        int addition() {
          int x;  // x is not initialized
          return x + 10; // Noncompliant: x has grabage value
        }
        
        int dereference() {
          int* p; // p is not initialized
          return *p; // Noncompliant: p has garbage value
        }
        

        Similarly, structures that simply aggregate variables of built-in types, such as arrays or struct/class types without a constructor, will not initialize their members when declared without an initializer:

        struct Aggregate {
          int i;
          float f;
        };
        
        void aggregates() {
          int* intArray[5]; // each element of array is not initializer
          Aggregate aggr; // members aggr.i, agrr.f are not initialized
          Aggregate aggrArray[2]; // members of each element are not initialized
        }
        

        Finally, allocating objects of builtin or such aggregates types on the heap, also does not initialize their values:

        void usingMalloc() {
          int* intArr = (int*)malloc(sizeof(int) * 10); // each of 10 allocated integers is not initialized
        }
        

        This also applies when new is used in C++:

        void usingNew() {
          Aggregate* aggrPtr = new Aggregate; // members of allocated Aggregate are not initialized
          Aggregate* aggrArr = new Aggregate[5]; // members of each of 5 Aggregate objects are not initialized
        }
        

        What is the potential impact?

        Using garbage values will cause the program to behave nondeterministically at runtime. The program may produce a different output or crash depending on the run.

        In some situations, loading a variable may expose sensitive data, such as a password that was previously stored in the same location, leading to a vulnerability that uses such a defect as a gadget for extracting information from the instance of the program.

        Finally, in C++, outside of a few exceptions related to the uses of unsigned char or std::byte, loading data from an uninitialized variable causes undefined behavior. This means that the compiler is not bound by the language standard anymore, and the program has no meaning assigned to it. As a consequence, the impact of such a defect is not limited to the use of garbage values.

        Why is there an issue for a class with a default constructor?

        In C++, a class can define a default constructor invoked when an object of the given type is created. Such a constructor is called even if a variable is declared without any initializer. However, if the constructor code omits the initialization of a member that itself does not have the default constructor, the member will remain uninitialized (See also S2107). And reading from it will produce a garbage value:

        struct Partial {
          // x is not initialized
          Partial() : y(10.0) {}
        
          int x;
          float y;
        };
        
        int initialized() {
          Partial p; // constructor is called
          // or even Partial p{};
          return p.x; // Non-compliant: reading an uninitialized variable
        }
        

        Exceptions

        This rule does not flag the variables with static storage duration, meaning: global, static, and thread-local variables.

        All the variables with static storage duration are zero-initialized before the initializer is evaluated. As a consequence, any variable or member of such an object has a defined value even if no initializer is specified.

        int globInt;
        int globTab[10];
        Aggregate globAggr;
        Partial globPart; // x member is zero-initialized
        
        int uses() {
          static int staticInt;
          return globInt     // Compliant: all zero-initialized
               + globTab[2]
               + globAggr.f
               + globPart.x
               + staticInt;
        }
        
          Available In:
        • SonarQube IdeCatch issues on the fly,
          in your IDE
        • SonarQube CloudDetect issues in your GitHub, Azure DevOps Services, Bitbucket Cloud, GitLab repositories
        • SonarQube ServerAnalyze code in your
          on-premise CI
          Developer Edition
          Available Since
          9.1

        © 2008-2025 SonarSource SA. All rights reserved.

        Privacy Policy | Cookie Policy | Terms of Use