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
 
Tags
    Impact
      Clean code attribute
        1. "abort", "exit", "getenv" and "system" from <stdlib.h> should not be used

           Bug
        2. "atof", "atoi" and "atol" from <stdlib.h> should not be used

           Bug
        3. "<signal.h>" should not be used

           Bug
        4. Dynamic heap memory allocation should not be used

           Bug
        5. Lines starting with "#" should contain valid preprocessing directives

           Bug
        6. Macros used in preprocessor directives should be defined before use

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

           Bug
        8. "#include" directives should be followed by either <filename> or "filename" sequences

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

           Bug
        10. 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
        11. Function exit paths should have appropriate return values

           Bug
        12. Evaluation of the operand to the sizeof operator shall not contain side effects

           Bug
        13. Non-empty statements should change control flow or have at least one side-effect

           Bug
        14. Unary minus should not be applied to an unsigned expression

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

           Bug
        16. Boolean operations should not have numeric operands, and vice versa

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

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

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

           Bug
        20. 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
        21. Variables should be initialized before use

           Bug
        22. String literals with different prefixes should not be concatenated

           Bug
        23. Only escape sequences defined in the ISO C standard should be used

           Bug
        24. Macro arguments should not contain preprocessing directives

           Bug
        25. Variables should not be accessed outside of their scope

           Bug
        26. Coroutines should have well-defined exception behavior

           Bug
        27. The result of "make_format_args" should be passed directly as an argument

           Bug
        28. "std::format" numeric types should be 0-padded using the numerical padding and not the character padding

           Bug
        29. Arguments corresponding to width and precision formatting options should be integers

           Bug
        30. "std::format" should not be missing indexes

           Bug
        31. Type-constraints should not be used for forwarding reference parameters

           Bug
        32. Perfect forwarding constructors should be constrained

           Bug
        33. Requires-expression should not contain unevaluated concept checks or type predicates

           Bug
        34. Assigning to an optional should directly target the optional

           Bug
        35. Coroutine should have co_return on each execution path or provide return_void

           Bug
        36. Well-defined type-punning method should be used instead of a union-based one

           Bug
        37. Result of the standard remove algorithms should not be ignored

           Bug
        38. "std::cmp_*" functions should be used to compare unsigned values with negative values

           Bug
        39. "volatile" should not be used to qualify objects for which the meaning is not defined

           Bug
        40. "volatile" types should not be used in compound operations

           Bug
        41. "std::is_constant_evaluated" and "if consteval" should only be used when necessary

           Bug
        42. Heterogeneous sorted containers should only be used with types that support heterogeneous comparison

           Bug
        43. "std::scoped_lock" should be created with constructor arguments

           Bug
        44. Values returned from string find-related methods should not be treated as boolean

           Bug
        45. Objects should not be sliced

           Bug
        46. Relational and subtraction operators should not be used with pointers to different arrays

           Bug
        47. Arguments evaluation order should not be relied on

           Bug
        48. Immediately dangling references and pointers should not be created

           Bug
        49. "#pragma pack" should be used correctly

           Bug
        50. Enums should be consistent with the bit fields they initialize

           Bug
        51. "pthread_mutex_t" should be unlocked in the reverse order they were locked

           Bug
        52. "pthread_mutex_t" should be properly initialized and destroyed

           Bug
        53. "pthread_mutex_t" should not be locked when already locked, or unlocked when already unlocked

           Bug
        54. "std::move" and "std::forward" should not be confused

           Bug
        55. A call to "wait()" on a "std::condition_variable" should have a condition

           Bug
        56. Each operand of the ! operator, the logical && or the logical || operators shall have type bool

           Bug
        57. A pointer to a virtual base class shall only be cast to a pointer to a derived class by means of dynamic_cast

           Bug
        58. When an array is declared, its size shall either be stated explicitly or defined implicitly by initialization

           Bug
        59. "reinterpret_cast" should be used carefully

           Bug
        60. User-defined types should not be passed as variadic arguments

           Bug
        61. Class members should not be initialized with dangling references

           Bug
        62. Functions with "noreturn" attribute should not return

           Bug
        63. RAII objects should not be temporary

           Bug
        64. "memcmp" should only be called with pointers to trivially copyable types with no padding

           Bug
        65. "memcpy", "memmove", and "memset" should only be called with pointers to trivially copyable types

           Bug
        66. "std::auto_ptr" should not be used

           Bug
        67. Array values should not be replaced unconditionally

           Bug
        68. Integral operations should not overflow

           Bug
        69. "case" ranges should not be empty

           Bug
        70. All branches in a conditional structure should not have exactly the same implementation

           Bug
        71. Parameter values should be appropriate

           Bug
        72. "extern" shouldn't be used on member definitions

           Bug
        73. Declaration specifiers should not be redundant

           Bug
        74. Destructors should be "noexcept"

           Bug
        75. Stack allocated memory and non-owned memory should not be freed

           Bug
        76. Closed resources should not be accessed

           Bug
        77. Dynamically allocated memory should be released

           Bug
        78. Freed memory should not be used

           Bug
        79. Memory locations should not be released more than once

           Bug
        80. Memory access should be explicitly bounded to prevent buffer overflows

           Bug
        81. Zero should not be a possible denominator

           Bug
        82. Function declarations that look like variable declarations should not be used

           Bug
        83. "sizeof" should not be called on pointers

           Bug
        84. "const" references to numbers should not be made

           Bug
        85. Unary prefix operators should not be repeated

           Bug
        86. Non-existent operators like "=+" should not be used

           Bug
        87. Values of different "enum" types should not be compared

           Bug
        88. The "sizeof" and "alignof" operator should not be used with operands of a "void" type

           Bug
        89. "nonnull" parameters and return values of "returns_nonnull" functions should not be null

           Bug
        90. Conditionally executed code should be reachable

           Bug
        91. Line-splicing should not be used in "//" comments

           Bug
        92. Printf-style format strings should not lead to unexpected behavior at runtime

           Bug
        93. Null pointers should not be dereferenced

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

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

           Bug
        96. Recursion should not be infinite

           Bug
        97. Values should not be uselessly incremented

           Bug
        98. Member variables should be initialized

           Bug
        99. Resources should be closed

           Bug
        100. Line continuation characters '\' should not be followed by trailing whitespace

           Bug
        101. "sizeof(sizeof(...))" should not be used

           Bug
        102. Related "if/else if" statements should not have the same condition

           Bug
        103. Pointers should not be cast to integral types

           Bug
        104. Identical expressions should not be used on both sides of a binary operator

           Bug
        105. All code should be reachable

           Bug
        106. Loops with at most one iteration should be refactored

           Bug
        107. The original exception object should be rethrown

           Bug
        108. Variables should not be self-assigned

           Bug
        109. "operator delete" should be written along with "operator new"

           Bug
        110. Floating point numbers should not be tested for equality

           Bug
        111. Appropriate memory de-allocation should be used

           Bug
        112. Destructors should not throw exceptions

           Bug
        113. Condition-specific "catch" handlers should not be used after the ellipsis (catch-all) handler

           Bug
        114. Handlers in a single try-catch or function-try-block for a derived class and some or all of its bases should be ordered most-derived-first

           Bug
        115. Exception classes should be caught by reference

           Bug
        116. Handlers of a function-try-block implementation of a class constructor or destructor shall not reference non-static members from this class or its bases

           Bug
        117. Empty throws ("throw;") should only be used in the compound statements of catch handlers

           Bug
        118. An exception object should not have pointer type

           Bug
        119. Accessible base classes should not be both "virtual" and non-virtual in the same hierarchy

           Bug
        120. Multiple declarations for an identifier in the same namespace shall not straddle a using-declaration for that identifier

           Bug
        121. An object shall not be accessed outside of its lifetime

           Bug
        122. A function declared with the "[[noreturn]]" attribute shall not return

           Bug
        123. An "explicit type conversion" shall not be an "expression statement"

           Bug
        124. Reads and writes on the same file stream shall be separated by a positioning operation

           Bug
        125. Line-splicing shall not be used in "//" comments

           Bug
        126. A pointer to an incomplete "class" type shall not be deleted

           Bug
        127. The result of "std::remove", "std::remove_if", "std::unique" and "empty" shall be "used"

           Bug
        128. A comparison of a "potentially virtual" pointer to member function shall only be with "nullptr"

           Bug
        129. The "#include" directive shall be followed by either a "<filename>" or ""filename"" sequence

           Bug
        130. A "noexcept" function should not attempt to propagate an exception to the calling function

           Bug
        131. An exception of "class" type shall be caught by "const" reference or reference

           Bug
        132. Handlers for a "function-try-block" of a constructor or destructor shall not refer to non-static members from their class or its bases

           Bug
        133. An exception object shall not have pointer type

           Bug
        134. A named bit-field with "signed integer type" shall not have a length of one bit

           Bug
        135. The value of an object must not be read before it has been set

           Bug
        136. A virtual base class shall only be cast to a derived class by means of "dynamic_cast"

           Bug
        137. A line whose first token is "#" shall be a valid preprocessing directive

           Bug
        138. All identifiers used in the controlling expression of "#if" or "#elif" preprocessing directives shall be defined prior to evaluation

           Bug
        139. Tokens that look like a preprocessing directive shall not occur within a macro argument

           Bug

        Heterogeneous sorted containers should only be used with types that support heterogeneous comparison

        intentionality - efficient
        reliability
        Bug
        • performance
        • since-c++14

        Why is this an issue?

        Heterogeneous containers were introduced in C++14 to increase performance when working with std::map, std::set, std::multimap, std::multiset, and since C++20, their unordered_ counterparts. Before their introduction, when working with a key in such a container, it was required to use an object of the exact key type. This could lead to costly object creations when we would like to work with objects of compatible but different types:

        std::map<std::string, int> m;
        m.find("abc"); // Will convert the char const * to a std::string, maybe performing costly memory allocation
        

        With heterogeneous containers, the previous code will not create a string, but directly compare keys in the map with the searched char const *. This is a behavior you can opt-in with providing a transparent comparison method for the map.

        std::map<std::string, int, std::less<>> m; // std::less<> is transparent, std::less<anything> is not
        m.find("abc"); // Will directly compare the char const* with the keys, no object conversion
        

        A transparent comparator is one that declares a nested type named is_transparent. It is supposed to be able to compare heterogeneous object types, for instance in this case compare a char const * with a string, without performing any conversion.

        What happens if it cannot? Well, if the types are convertible with each other (which is usually the case when you want to work with a heterogeneous container), a conversion will happen, and a homogeneous comparison will be performed instead.

        class MyString {
        public:
          operator std::string() const {return myData;} // Converts to a string
          // Other functions to make the class behave correctly
        private:
          std::string myData;
        };
        bool operator==(const std::string&, const std::string&);
        bool operator<(const std::string&, const std::string&);
        
        void f() {
          std::map<std::string, int, std::less<>> m;
          MyString str{"abc"};
          m.find(str);
        }
        

        In this case, str will not be converted to a std::string when calling the function find (this function is now a member function template). However, each time during the search when a comparison will be needed between MyString and std::string, a conversion will now happen to convert str to always the same string. A unique conversion is replaced by O(ln N) conversions.

        This problem appears also for the case of an unordered container for which heterogeneous lookup is enabled (available since C++20), for which equality is used to compare elements in the same bucket (having same or close hashes):

        void g() {
          std::unordered_map<std::string, int, StringHash, std::equal_to<>> m;
          MyString str{"abc"};
          m.find(str);
        }
        

        In this case str will not be converted to a std::string when calling the function find, and each element of the bucket that corresponds to the hash of str will be compared using homogeneous operator==, and for each such comparison, a conversion will now happen. The number of compared elements varies depending on the hash distribution from O(1) (on average) to O(N) (in the worst case). As consequence, the performance of slow runs (when multiple hash collisions happen due to the data distribution) is made even worse.

        This rule raises an issue when a transparent lookup function of a heterogeneous container is used with a type that cannot be directly compared with the container key type. Only standard associative containers with expensive to create key and straightforward comparison functions are considered.

        Noncompliant code example

        void f() {
          std::map<std::string, int, std::less<>> m;
          MyString str{"abc"}; // See previous definition of MyString
          m.find(str); // Noncompliant, O(ln N) conversions
        }
        
        void g() {
          std::unordered_map<std::string, int, StringHash, std::equal_to<>> m;
          MyString str{"abc"}; // See previous definition of MyString
          m.find(str); // Noncompliant, up to O(N) conversions
        }
        

        Compliant solution

        Option 1: Make the container non-heterogeneous

        void f() {
          std::map<std::string, int> m;
          MyString str{"abc"}; // See previous definition of MyString
          m.find(str); // Compliant, one conversion at the start
        }
        
        void g() {
          std::unordered_map<std::string, int> m;
          MyString str{"abc"}; // See previous definition of MyString
          m.find(str); // Compliant, one conversion at the start
        }
        

        Option 2: Provide heterogeneous comparisons

        bool operator==(const MyString &s1, const std::string &s2) {/*...*/} // invoked for reversed order of arguments since {cpp}20
        bool operator<(const MyString &s1, const std::string &s2) {/*...*/}
        bool operator<(const std::string &s1, const MyString &s2) {/*...*/}
        void f() {
          std::map<std::string, int, std::less<>> m;
          MyString str{"abc"}; // See previous definition of MyString
          m.find(str); // Compliant, no conversion at all
        }
        
        void g() {
          std::unordered_map<std::string, int, StringHash, std::equal_to<>> m;
          MyString str{"abc"}; // See previous definition of MyString
          m.find(str); // Compliant, no conversion for equality and possibly one for hash computation
        }
        
          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