Why is this an issue?
This is a draft version of a MISRA C++ 202x rule proposed for public review.
MISRA Rule 9.6.3
Analysis Type: Decidable,Single Translation Unit
The unconstrained use of
goto can lead to programs that are extremely difficult to comprehend and analyse. However, flags may need to
be introduced to give the required control flow when it is not used, with the possibility that the flags may themselves make the code less transparent
goto were used. The restricted use of
goto is therefore allowed where that use will not lead to semantics contrary
to developer expectations.
This rule prohibits the use of back jumps as they can be used to introduce iteration without using the well-defined iteration statements supplied
by the language.
Note: the C++ Standard places restrictions on the uses of forward jumps. For example, it is not permitted to jump from a point
where a local variable with initialization is not in scope to a point where it is in scope.
int32_t x = 0;
if ( x == 10 )
goto L2; // Compliant
goto L1; // Non-compliant
Copyright The MISRA Consortium Limited © 2023
- S999 targets the same defect as this rule but for a non-mission-critical context.
- M23_403: MISRA C++ 2023 Rule 9.6.1 - The
goto statement should not be used
- MISRA C++ 2023 Rule 9.6.2 - A
goto statement shall reference a label in a surrounding block