Rule Definition
Unconstrained use of goto can lead to programs that are extremely difficult to comprehend, analyze and, for C++, can also lead to the program exhibiting unspecified behavior. 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. “Back” jumps are prohibited as they can be used to create iterations without using the well-defined iteration statements supplied by the core language
Remediation
Do not use goto to create iterations, instead use defined methods for such purpose
Violation Code Sample
void f ( void )
{
int32_t j = 0;
L1:
++j;
if ( 10 == j )
{
goto L2; /* Compliant */
}
goto L1; /* Non-compliant */
L2 :
++j;
}
Reference
Standards References:
MISRA C++ 2008, 6-6-2: The goto statement shall jump to a label declared later in the same function body
MISRA C 2012, 15.2: The goto statement shall jump to a label declared later in the same function
Related Technologies
Technical Criterion
Programming Practices - Structuredness
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.