MISRA C++ 9.6.2 - Printable Version +- MISRA Discussion Forums (https://forum.misra.org.uk) +-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18) +--- Forum: MISRA C++:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=188) +---- Forum: 4.9 Statements (https://forum.misra.org.uk/forumdisplay.php?fid=195) +---- Thread: MISRA C++ 9.6.2 (/showthread.php?tid=1695) |
MISRA C++ 9.6.2 - gdavis - 06-07-2024 Hello, My question is about the third example for MISRA C++ 2023 Rule 9.6.2: "A goto statement shall reference a label in a surrounding block". void f3(int32_t i, int32_t x, int32_t y) { switch( i ) { // statement A case 0: if ( x < y ) goto L3; // Non-compliant break; case 1: L3: break; } } I am not sure why this is non-compliant. In terms of the amplification of the rule, "A goto statement shall be enclosed in a statement that directly encloses its reference label", statement A is the compound statement as shown above. I believe the goto statement is enclosed within statement A, and statement A directly-encloses the reference label (L3). The MISRA C 2012 and MISRA C 2023 versions of the equivalent rule 15.3 provide a different amplification that directly addresses this: "A switch-clause that does not consist of a compound statement is treated as if it were a block" Please let me know what you think. Thank you. RE: MISRA C++ 9.6.2 - misra cpp - 12-07-2024 You're right that the goto is enclosed by the IF, the labelled statement case 0 and the switch body, however L3 is only directly enclosed by the labelled statement case 1 - so is not allowed by this rule. In addition, the label L3 is part of a switch label group, which is not allowed by 9.4.2 amplification item 3. |