V2536. MISRA. Function should not contain labels not used by any 'goto' statements.


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

The presence of labels not referenced by any 'goto' operator in the body of the function might indicate an error made by a programmer. For example, such labels can appear if a programmer accidentally used the wrong label jump or made a typo when creating a case-label.

Let's consider the first example:

string SomeFunc(const string &fStr)
{
  string str;
  while (true)
  {
    getline(cin, str); 
    if (str == fStr)
      goto retRes;
    else if (str == "stop")
      goto retRes;
  }
retRes:
  return str;
badRet:
  return "fail";
}

In the body of the function there is the 'badRet' label, not referenced by any 'goto' operator, however there is another referenced 'retRes' label. A programmer made a mistake and instead of going to the 'badRet' label, he jumped to the 'retRes' label again.

The correct code should be as follows:

string SomeFunc(const string &fStr)
{
  string str;
  while(true)
  {
    getline(cin,str); 
    if (str == fStr)
      goto retRes;
    else if(str == "stop")
      goto badRet;
  }
retRes:
  return str;
badRet:
  return "fail";
}

Let's consider the second example:

switch (c)
{
case 0:
  ...
  break;
case1:  // <=
  ...
  break;
defalt: // <=
  ...
  break;
}

A programmer made two typos when writing the 'switch' body which resulted in two labels, not referenced by any 'goto' operator. On top of this rule violation by these labels, the code that contains them turned out to be unreachable.

Fixed code:

switch (c)
{
case 0:
  ...
  break;
case 1:
  ...
  break;
default:
  ...
  break;
}

Bugs Found

Checked Projects
354
Collected Errors
13 290