Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.GOTO.NESTED.2012

Label referenced by goto is not in this or enclosing block.

MISRA C 2012 Rule 15.3: Any label referenced by a goto statement shall be declared in the same block, or in any block enclosing the goto statement

Category: Required

Analysis: Decidable, Single Translation Unit

Applies to: C90, C99

Amplification

For the purposes of this rule, a switch-clause that does not consist of a compound statement is treated as if it were a block.

Rationale

Unconstrained use of goto can lead to programs that are unstructured and extremely difficult to understand.

Preventing jumps between blocks, or into nested blocks, helps to minimize visual code complexity.

Note: C99 is more restrictive when variably modified types are used. An attempt to make a jump from outside the scope of an identifier with a variably modified type into such a scope results in a constraint violation.

Example

void f1 ( int32_t a )
{
    if ( a <= 0 )
    {
        goto L2;   /* Non-compliant */
    }

    goto L1;       /* Compliant     */

    if ( a == 0 )
    {
        goto L1;   /* Compliant     */
    }
    goto L2;       /* Non-compliant */

L1:
    if ( a > 0 )
    {
    L2:
      ;
    }
}

In the following example, the label L1 is defined in a block which encloses the block containing the goto statement. However, the jump crosses from one switch-clause to another and, since switch-clauses are treated like blocks for the purposes of this rule, the goto statement is non-compliant.

switch ( x )
{
    case 0:
       if ( x == y )
       {
           goto L1;
       }
       break;
    case 1:
        y = x;
L1:
    ++x;
    break;
  default:
    break;
}