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: 5 rules found
misra-mandatory
    Impact
      Clean code attribute
        1. The pointers returned by the C++ Standard Library functions "localeconv", "getenv", "setlocale" or "strerror" must only be used as if they have pointer to const-qualified type

           Bug
        2. The pointer returned by the C++ Standard Library functions "asctime", "ctime", "gmtime", "localtime", "localeconv", "getenv", "setlocale" or "strerror" must not be used following a subsequent call to the same function

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

           Bug
        4. A function must not return a reference or a pointer to a local variable with automatic storage duration

           Bug
        5. An object or subobject must not be copied to an overlapping object

           Bug

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

        intentionality - logical
        reliability
        Bug
        • pitfall
        • misra-c++2023
        • misra-mandatory

        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.8.2 - A function must not return a reference or a pointer to a local variable with automatic storage duration

        Category: Mandatory

        Analysis: Decidable,Single Translation Unit

        Amplification

        This rule also applies to:

        • Function parameters passed by value; and
        • Returning a lambda that captures by reference a variable with automatic storage duration; and
        • Returning a lambda that captures the address of a variable with automatic storage duration.

        For the purposes of this rule, a throw that is not caught within the function is considered to be a return from the function.

        Rationale

        Automatic variables are destroyed when a function returns. Returning a reference or pointer to such a variable allows it to be used after its destruction, leading to undefined behaviour.

        Note: this rule and M23_360: MISRA C++ 2023 Rule 6.8.1.

        Example

        int32_t * f1()
        {
          int32_t x = 99;
        
          return &x;                 // Non-compliant
        }
        
        int32_t * f2( int32_t y )
        {
          return &y;                 // Non-compliant
        }
        
        int32_t & f3()
        {
          int32_t x = 99;
        
          return x;                  // Non-compliant
        }
        
        int32_t & f4( int32_t y )
        {
           return y;                 // Non-compliant
        }
        
        int32_t & f5( int32_t & x )
        {
          return x;                  // Rule does not apply
        }
        
        int32_t * f6()
        {
          static int32_t x = 0;
        
          return &x;                 // Rule does not apply
        }
        
        void f7()
        {
          int32_t x = 0;
        
          throw &x;                  // Non-compliant
        }
        
        void f8()
        {
          try
          {
            int32_t x = 0;
        
            throw &x;                // Rule does not apply - caught within this function
          }
          catch ( ... )
          {
          }
        }
        
        auto f9()
        {
          int32_t x { 42 };
        
          return [&x]() {};          // Non-compliant - captures local by reference
        }
        
        auto f10()
        {
          int32_t x { 42 };
        
          return [p = &x]() {};      // Non-compliant - captures address of local
        }
        

        The following example is compliant with this rule, but violates M23_360: MISRA C++ 2023 Rule 6.8.1.

        int32_t * f11()
        {
          int32_t   i = 42;
          int32_t * p = &i;
        
          return p;                  // Compliant with this rule
        }
        

        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