V2529. MISRA. Any label should be declared in the same block as 'goto' statement or in any block enclosing it.


This diagnostic rule is based on the software development guidelines developed by MISRA (Motor Industry Software Reliability Association).

Excessive use of 'goto' statements complicates the code structure and obscures the code.

To make the code clearer, it is recommended that you discard jumps to nested blocks or between blocks of the same level.

Example 1:

void V2532_pos1()
{
  ...
  goto label;
  ...
  {
  label:
    ...
  }
}

The 'goto' statement here refers control to a nested block, which makes this code non-compliant.

No warning will be produced on the following code:

void V2532_neg1()
{
  ...
  label:
  ...
  {
    goto label;
    ...
  }
}

Note: the bodies of switch labels are considered composite statements even if they are not enclosed in braces. For this reason, jumps to the body of a switch label from outer code and jumps between different switch labels do not comply with the rule.
Consider the following examples.

Jumping to a switch label from outer code (non-compliant):

void V2532_pos2(int param)
{
  goto label;

  switch (param)
  {
  case 0:
    break;
  default:
  label:;
    break;
  }
}

Jumping between switch labels (non-compliant):

void V2532_pos3(int param)
{
  switch (param)
  {
  case 0:
    goto label;
    break;
  default:
  label:
    break;
  }
}

Jumping from a switch label to outer code (OK):

void V2532_neg2(int param)
{
  label:
  switch (param)
  {
  case 0:
    goto label;
    break;
  default:
    break;
  }
}

Jumping within the bounds of one switch label (OK):

void neg3(int param)
{
  switch (param)
  {
  case 0:
  {
    ...
    {
      goto label;
    }
  }
  label:
    break;
  default:
    break;
  }
}

Bugs Found

Checked Projects
336
Collected Errors
12 745