Uploaded image for project: 'SonarCFamily'
  1. SonarCFamily
  2. CPP-173

Rule MISRA 6-6-1 : Any label referenced by a goto statement shall be declared in the same block, or in a block enclosing the goto statement.

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.2
    • Component/s: MISRA, Rules
    • Labels:
      None

      Description

      • Title
        Any label referenced by a goto statement shall be declared in the same block, or in a block enclosing the goto statement (MISRA 6-6-1)
      • Description
        Unconstrained use of goto can lead to programs that are extremely difficult to comprehend, analyse and, for C++, can also lead to the program exhibiting unspecified behaviour.
        However, in many cases a total ban on goto requires the introduction of flags to ensure correct control flow, and it is possible that these flags may themselves be less transparent than the goto they replace.
        Therefore, the restricted use of goto is allowed where that use will not lead to semantics contrary to developer expectations. Jumping in to nested blocks is prohibited as it may lead to complex flow graphs.

      Example

      void f1 ( )
      {
        int32_t j = 0;
        goto L1; 
        for ( j = 0; j < 10 ; ++j )
        {
      L1:                          // Non-compliant
            j;
        }
      }
      
      void f2 ( )
      {
        for ( int32_t j = 0; j < 10 ; ++j )
        { 
          for ( int32_t i = 0; i < 10; ++i )
          {
            goto L1; 
          }
        }
      L1:                                // Compliant
        f1 ( );	
      }
      
      • Violation message
        Move the label referenced by this goto statement outside of current block
      • Default priority
        Critical

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                julien.henry Julien Henry
                Reporter:
                freddy.mallet Freddy Mallet (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: