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
  • 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 315
  • Vulnerability13
  • Bug76
  • Security Hotspot19
  • Code Smell207

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

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

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

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

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

           Bug
        6. "errno" should not be used

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

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

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

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

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

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

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

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

           Bug
        15. Function-like macros should not be used

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

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

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

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

           Bug
        20. Unions should not be used

           Code Smell
        21. Structure and union types should be complete at the end of a translation unit

           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. Object declarations should contain no more than 2 levels of pointer indirection

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

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

           Bug
        26. The number of arguments passed to a function should match the number of parameters

           Bug
        27. Functions without parameters should be declared with parameter type "void"

           Code Smell
        28. Parameters in a function prototype should be named

           Code Smell
        29. Recursion should not be used

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

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

           Code Smell
        32. "continue" should not be used

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

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

           Bug
        35. Unused type declarations should be removed

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

           Code Smell
        37. Tests of non-Boolean values against zero should be explicit

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

           Code Smell
        39. Comma operator should not be used

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

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

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

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

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

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

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

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

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

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

           Code Smell
        50. 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
        51. The value of a complex expression should only be cast to a type that is narrower and of the same signedness as the underlying type of the expression

           Code Smell
        52. Signed and unsigned types should not be mixed in expressions

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

           Code Smell
        54. Variables should be initialized before use

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

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

           Code Smell
        57. Object and function types should be explicitly stated in their declarations and definitions

           Code Smell
        58. Functions should be declared explicitly

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

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

           Bug
        61. Bit fields should be declared with appropriate types

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

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

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

           Code Smell
        65. Digraphs should not be used

           Code Smell
        66. Trigraphs should not be used

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

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

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

           Code Smell
        70. Macro arguments should not contain preprocessing directives

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

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

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

           Code Smell
        74. Unevaluated operands should not have side effects

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

           Bug
        76. Keywords shall not be used as macros identifiers

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

           Code Smell
        78. Integral operations should not overflow

           Bug
        79. Conditionally executed code should be reachable

           Bug
        80. The "<stdlib.h>" functions "bsearch" and "qsort" should not be used

           Bug
        81. Octal and hexadecimal escape sequences should be terminated

           Code Smell
        82. Flexible array members should not be declared

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

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

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

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

           Code Smell
        87. "restrict" should not be used

           Code Smell
        88. "static" should not be used for the size of an array parameter

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

           Bug
        90. All code should be reachable

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

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

           Code Smell
        93. Octal values should not be used

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

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

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

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

           Code Smell
        98. "if ... else if" constructs should end with "else" clauses

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

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

           Bug
        101. Function parameters' initial values should not be ignored

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

           Code Smell
        103. Control structures should use curly braces

           Code Smell
        104. Unused function parameters should be removed

           Code Smell
        105. Unused functions and methods should be removed

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

           Code Smell
        107. Variables should not be shadowed

           Code Smell
        108. Empty statements should be removed

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

           Code Smell
        110. Unused labels should be removed

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

           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