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 420
  • Vulnerability14
  • Bug111
  • Security Hotspot19
  • Code Smell276

  • Quick Fix 27
Filtered: 64 rules found
pitfall
    Impact
      Clean code attribute
        1. "goto" should jump to labels declared later in the same function

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

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

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

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

           Code Smell
        6. Functions without parameters should be declared with parameter type "void"

           Code Smell
        7. Recursion should not be used

           Code Smell
        8. Functions should not be defined with a variable number of arguments

           Code Smell
        9. Constants of unsigned type should have a "U" suffix

           Code Smell
        10. If a function has internal linkage then all re-declarations shall include the static storage class specifer

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

           Code Smell
        12. Macro arguments should not contain preprocessing directives

           Bug
        13. All the elements of an aggregate should be provided with an initial value

           Code Smell
        14. Macros should not be used as replacements for "typedef" and "using"

           Code Smell
        15. Keywords shall not be used as macros identifiers

           Code Smell
        16. Logical operators should not be confused with bitwise operators

           Code Smell
        17. Include directives should not rely on non-portable search strategy

           Code Smell
        18. "#include" paths should be portable

           Code Smell
        19. Array indices should be placed between brackets

           Code Smell
        20. Bit fields should not be used

           Code Smell
        21. Conditionally executed code should be reachable

           Bug
        22. Control characters should not be used in literals

           Code Smell
        23. Octal and hexadecimal escape sequences should be terminated

           Code Smell
        24. Preprocessor directives should not be indented

           Code Smell
        25. "goto" statements should not be used to jump into blocks

           Code Smell
        26. Related "if/else if" statements should not have the same condition

           Bug
        27. "static" should not be used for the size of an array parameter

           Code Smell
        28. "switch" statements should not be nested

           Code Smell
        29. The sign of an unsigned variable should not be tested

           Code Smell
        30. Octal values should not be used

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

           Code Smell
        32. Control structures should use curly braces

           Code Smell
        33. Variables should not be shadowed

           Code Smell
        34. Control should not be transferred into a complex logic block using a "goto" or a "switch" statement

           Code Smell
        35. An "integer-literal" of type "long long" shall not use a single "L" or "l" in any suffix

           Code Smell
        36. "Integral promotion" and the "usual arithmetic conversions" shall not change the signedness or the "type category" of an operand

           Code Smell
        37. The library function "system" from "<cstdlib>" shall not be used

           Vulnerability
        38. All variables should be initialized

           Code Smell
        39. Octal escape sequences, hexadecimal escape sequences and universal character names shall be terminated

           Code Smell
        40. Dynamic memory shall be managed automatically

           Code Smell
        41. The facilities provided by the standard "header file" "<csignal>" shall not be used

           Code Smell
        42. The macro "offsetof" shall not be used

           Code Smell
        43. The standard "header file" "<csetjmp>" shall not be used

           Code Smell
        44. Precautions shall be taken in order to prevent the contents of a "header file" being included more than once

           Code Smell
        45. Program-terminating functions should not be used

           Code Smell
        46. A named bit-field with "signed integer type" shall not have a length of one bit

           Bug
        47. A bit-field shall have an appropriate type

           Code Smell
        48. Bit-fields should not be declared

           Code Smell
        49. The features of "<cstdarg>" shall not be used

           Code Smell
        50. Functions shall not call themselves, either directly or indirectly

           Code Smell
        51. A function must not return a reference or a pointer to a local variable with automatic storage duration

           Bug
        52. The "goto" statement shall jump to a label declared later in the function body

           Code Smell
        53. A "goto" statement shall reference a label in a surrounding block

           Code Smell
        54. The body of an "iteration-statement" or a "selection-statement" shall be a "compound-statement"

           Code Smell
        55. The comma operator should not be used

           Code Smell
        56. The "declaration" of an object should contain no more than two levels of pointer indirection

           Code Smell
        57. The names of the "standard signed integer types" and "standard unsigned integer types" should not be used

           Code Smell
        58. A "header file" shall not contain definitions of functions or objects that are non-inline and have external linkage

           Code Smell
        59. Tokens that look like a preprocessing directive shall not occur within a macro argument

           Bug
        60. Function-like macros shall not be defined

           Code Smell
        61. The lowercase form of "L" shall not be used as the first character in a literal suffix

           Code Smell
        62. Unsigned "integer literals" shall be appropriately suffixed

           Code Smell
        63. Octal constants shall not be used

           Code Smell
        64. A variable declared in an "inner scope" shall not hide a variable declared in an "outer scope"

           Code Smell

        A variable declared in an "inner scope" shall not hide a variable declared in an "outer scope"

        intentionality - clear
        maintainability
        Code Smell
        • confusing
        • pitfall
        • misra-c++2023
        • misra-required

        Why is this an issue?

        More Info

        This rule is part of MISRA C++:2023.

        Usage of this content is governed by Sonar’s terms and conditions. Redistribution is prohibited.

        Rule 6.4.1 - A variable declared in an inner scope shall not hide a variable declared in an outer scope

        Category: Required

        Analysis: Decidable,Single Translation Unit

        Amplification

        A variable declaration [1] in an inner scope is considered to hide a variable in an outer scope when it has the same name and the variable in the outer scope would be found by name lookup in the inner scope at a point immediately before the declaration [1].

        The terms outer scope and inner scope are defined as follows:

        • The global scope is the outermost scope;
        • Each block (compound-statement), namespace or class introduces an inner scope;
        • In a function definition, the function parameters have the same scope as the corresponding function body (compound-statement or function-try-block);
        • A derived class is treated as an inner scope with respect to the base class;
        • The definition of a member function introduces an inner scope to the class’s definition;
        • The selection-statement_s and _iteration-statement_s introduce an _inner scope which contains the controlled statement(s) and corresponding condition and init-statement.

        If declarations [1] from a namespace are introduced into a scope by a using-declaration, then they are treated as though they were declared in that scope.

        For the purposes of this rule, the following are treated as the declaration [1] of variables:

        • All data member and function parameter declarations [1]; and
        • The enumerators of an unscoped enumeration type [2] (which have the same scope as the enumeration type).

        Rationale

        Identifier hiding may lead to developer confusion.

        Note: this rule prevents the name of a global variable from being reused as the name of a local variable.

        Exception

        A class constructor may have a parameter with the same name as a member variable, provided the only use made of that parameter is to initialize the member. This is a common idiom that poses no risk.

        Example

        int16_t i;
        
        void f1()
        {
          int32_t i;                // Non-compliant - hides i in global scope
          int32_t z;
        
          if ( i == 3 )             // It could be confusing as to which i this refers
          {
            int32_t z;              // Non-compliant - hides z before if
          }
        }
        
        void f2( int8_t i )         // Non-compliant - hides i in global scope
        {
        }
        
        class C
        {
          float i;                  // Non-compliant - hides i in global scope
          float j;
        
        public:
          C ( float j )             // Compliant by exception
            : j ( j ) {}
        
          C ( float j, float k )
            : j ( j )
          {
            j += k;                 // Non-compliant - 'j' hides C::j
          }
        
          void f3()
          {
            int32_t j = 0;          // Non-compliant - hides C::j
          }
        };
        
        namespace NS1
        {
          int32_t i;                // Non-compliant - hides i in global scope
        
          void f4( int32_t j )      // Compliant - parameter j does not hide C::j
          {
            int32_t l = i + j;      // Compiles using ::i if NS1::i declaration removed
          }
        }
        
        namespace NS2
        {
          int32_t v;
        }
        
        using NS2::v;
        
        void f5()
        {
          float v;                  // Non-compliant - using hides NS2::v in global scope
        }
        
        enum E { e0, e1, e2 };
        
        namespace
        {
          int32_t e1 = 32;          // Non-compliant - hides e1 member of E (in global
        }                           // scope)
        

        Note that compiler reporting of a redeclaration [3] error against para is inconsistent for the following example:

        int16_t f6( int16_t para )  // 'para' has same scope as function body
        try
        {                           // Inner scope within function body
          int16_t para = 1;         // Non-compliant - hides parameter
          int16_t a    = 2;
        
          return para + a;
        }
        
        catch( ... )
        {                           // Inner scope within function body
          int16_t para = 1;         // Non-compliant - hides parameter
          int16_t a    = 2;
        
          return para + a;
        }
        
        void f7( int32_t i )
        {
          for ( int32_t i = 0; i < 9; ++i ) {}   // Non-compliant
        
          for ( int32_t j = 0; j < i; ++j ) {}
        
          for ( int32_t j = 0; j < i; ++j ) {}   // Compliant - new scope
        
          for ( int32_t k = 0; k < i; ++k ) {}
        
          int32_t k = i;                         // Compliant - for-loop 'k' not in scope
        
          for ( int32_t k = 0; k < i; ++k ) {}   // Non-compliant - hides 'k' above
        
          if ( get() )                           // Introduces an inner scope into which 'k'
          {                                      //   is defined.
            int32_t k;                           // Non-compliant - hides 'k' in outer scope
          }
        }
        

        In the following example, there is no hiding in the compliant examples as the local

        variable z cannot be found by name lookup within the body of a lambda.

        void f8()
        {
          char z;
        
          auto L1 = [ z ](){ return z; };        // Compliant - no hiding
          auto L2 = []( char z ){ return z; };   // Compliant - no hiding
          auto L3 = [](){ char z { 'a' }; };     // Compliant - no hiding
          auto L4 = [ z ](){ char z { 'a' }; };  // Non-compliant - captured z is hidden
        }
        

        Glossary

        [1] Declaration

        A declaration introduces the name of an entity into a translation unit (see [basic.def]/1).

        An entity may be declared several times. The first declaration of an entity in a translation unit is called an introduction [4]. All subsequent declarations are called redeclarations [3].

        A definition [5] is a declaration, as described in [basic.def]/2.

        [2] Unscoped enumeration type

        A type created with the enum keyword that is not created as enum class or enum struct. Values of such a type will be subject to integral promotion.

        [3] Redeclaration

        See declaration [1].

        [4] Introduction

        See declaration [1].

        [5] Definition

        See declaration [1].

        Copyright The MISRA Consortium Limited © 2023

          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

        © 2025 SonarSource Sàrl. All rights reserved.

        Privacy Policy | Cookie Policy | Terms of Use