<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title><![CDATA[MISRA Discussion Forums - 4.9 Statements]]></title>
		<link>https://forum.misra.org.uk/</link>
		<description><![CDATA[MISRA Discussion Forums - https://forum.misra.org.uk]]></description>
		<pubDate>Tue, 10 Nov 2026 11:50:26 +0000</pubDate>
		<generator>MyBB</generator>
		<item>
			<title><![CDATA[Rule 9.5.1 - Missing definitions]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1759</link>
			<pubDate>Tue, 02 Dec 2025 08:55:55 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5614">cgpzs</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1759</guid>
			<description><![CDATA[Rule 9.5.1 talks about concepts like `init-statement`, `loop-counter`, etc which have not been defined anywhere in the document. `loop-counter` for example is a MISRA term and not a C++ Standard term.<br />
<br />
The old MISRA C++:2008 rules did precisely define these concepts before they were used.<br />
<br />
So I think MISRA C++:2023 should do the same.]]></description>
			<content:encoded><![CDATA[Rule 9.5.1 talks about concepts like `init-statement`, `loop-counter`, etc which have not been defined anywhere in the document. `loop-counter` for example is a MISRA term and not a C++ Standard term.<br />
<br />
The old MISRA C++:2008 rules did precisely define these concepts before they were used.<br />
<br />
So I think MISRA C++:2023 should do the same.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[9.4.2 assert(false) in default clause, compliant?]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1740</link>
			<pubDate>Tue, 03 Jun 2025 19:01:41 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5614">cgpzs</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1740</guid>
			<description><![CDATA[Hi,<br />
<br />
MISRA C++:2023 Rule 9.4.2 provides a number of requirements on switch statements, in particular that every branch shall be unconditionally terminated.<br />
<br />
Assuming that assertions are always enabled (also in Release builds), would this code be compliant with 9.4.2?<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>enum class Foo<br />
{<br />
&nbsp;&nbsp;a,<br />
&nbsp;&nbsp;b<br />
};<br />
<br />
int get(Foo f)<br />
{<br />
&nbsp;&nbsp;switch(f)<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;case Foo::a: { return 123; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;case Foo::b: { return 321; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;default: { assert(false &amp;&amp; "Unreachable"); }<br />
&nbsp;&nbsp;}<br />
}</code></div></div><br />
assert(false) leads to calling the assert_handler function, which is marked [[noreturn]], so it would fall under requirement 5f of the rule. Would you agree?<br />
<br />
Thanks!]]></description>
			<content:encoded><![CDATA[Hi,<br />
<br />
MISRA C++:2023 Rule 9.4.2 provides a number of requirements on switch statements, in particular that every branch shall be unconditionally terminated.<br />
<br />
Assuming that assertions are always enabled (also in Release builds), would this code be compliant with 9.4.2?<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>enum class Foo<br />
{<br />
&nbsp;&nbsp;a,<br />
&nbsp;&nbsp;b<br />
};<br />
<br />
int get(Foo f)<br />
{<br />
&nbsp;&nbsp;switch(f)<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;case Foo::a: { return 123; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;case Foo::b: { return 321; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;default: { assert(false &amp;&amp; "Unreachable"); }<br />
&nbsp;&nbsp;}<br />
}</code></div></div><br />
assert(false) leads to calling the assert_handler function, which is marked [[noreturn]], so it would fall under requirement 5f of the rule. Would you agree?<br />
<br />
Thanks!]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[9.4.2 - Requirement about the default clause]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1737</link>
			<pubDate>Tue, 20 May 2025 10:03:26 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5614">cgpzs</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1737</guid>
			<description><![CDATA[Hi,<br />
<br />
Rule 9.4.2 requires every switch statement to have a default clause, to match handling of if/else. However, unlike an if/else statement, this has a very serious implication: it prevents the compiler from warning about explicitly unhandled enumerators:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>enum class Enum<br />
{<br />
  A,<br />
  B,<br />
  C  // newly added<br />
};<br />
<br />
// some client code, far away from the Enum declaration<br />
int process(Enum e)<br />
{<br />
  switch(e)<br />
  {<br />
    case Enum::A: { return 123; }<br />
    case Enum::B: { return 321; }<br />
    // Oops, client code does not realize they should handle Enum::C, compiler didn't warn!<br />
    default: { return 0; }<br />
  }<br />
}</code></div></div><br />
One could put a runtime assertion in the `default` clause, but it only shifts the problem to the right. A problem that would have been caught at compile time can now only be caught at runtime. We should catch as many defects as possible at compile time.<br />
<br />
One can also enable -Wswitch-enum, but this is not always desirable. Sometimes one explicitly wants the switch to be non-exhaustive, and can express that via the "default" clause. With the warning, one needs to introduce non-compliant inline suppressions with compiler-specific pragmas. Or one must explicitly list all the unwanted labels and fall-through the default case, polluting the code with non-interesting code paths.<br />
<br />
Here we are balancing two risks:<br />
<br />
1. The risk of not handling a valid enumerator (as above).<br />
2. The risk of not handling an non-enumerator int casted into an Enum (as per the Rationale), in the spirit of defensive programming.<br />
<br />
Questions: has Risk 1 been considered by MISRA? Is there evidence suggesting that Risk 2 is more likely to happen than Risk 1, and we should therefore prioritize it instead?<br />
<br />
Thanks!]]></description>
			<content:encoded><![CDATA[Hi,<br />
<br />
Rule 9.4.2 requires every switch statement to have a default clause, to match handling of if/else. However, unlike an if/else statement, this has a very serious implication: it prevents the compiler from warning about explicitly unhandled enumerators:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>enum class Enum<br />
{<br />
  A,<br />
  B,<br />
  C  // newly added<br />
};<br />
<br />
// some client code, far away from the Enum declaration<br />
int process(Enum e)<br />
{<br />
  switch(e)<br />
  {<br />
    case Enum::A: { return 123; }<br />
    case Enum::B: { return 321; }<br />
    // Oops, client code does not realize they should handle Enum::C, compiler didn't warn!<br />
    default: { return 0; }<br />
  }<br />
}</code></div></div><br />
One could put a runtime assertion in the `default` clause, but it only shifts the problem to the right. A problem that would have been caught at compile time can now only be caught at runtime. We should catch as many defects as possible at compile time.<br />
<br />
One can also enable -Wswitch-enum, but this is not always desirable. Sometimes one explicitly wants the switch to be non-exhaustive, and can express that via the "default" clause. With the warning, one needs to introduce non-compliant inline suppressions with compiler-specific pragmas. Or one must explicitly list all the unwanted labels and fall-through the default case, polluting the code with non-interesting code paths.<br />
<br />
Here we are balancing two risks:<br />
<br />
1. The risk of not handling a valid enumerator (as above).<br />
2. The risk of not handling an non-enumerator int casted into an Enum (as per the Rationale), in the spirit of defensive programming.<br />
<br />
Questions: has Risk 1 been considered by MISRA? Is there evidence suggesting that Risk 2 is more likely to happen than Risk 1, and we should therefore prioritize it instead?<br />
<br />
Thanks!]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 9.3.1 - iteration statement followed by 'try']]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1734</link>
			<pubDate>Fri, 28 Mar 2025 06:19:27 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5807">mstaron</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1734</guid>
			<description><![CDATA[It is possible to add the 'try' statement directly after 'while', for example:<br />
<br />
<span style="color: #000000;" class="mycode_color"><span style="font-family: Consolas, 'Liberation Mono', Courier, monospace, Consolas, 'Courier New', monospace;" class="mycode_font"><span style="color: #0000ff;" class="mycode_color">class</span> Exception{};<br />
<span style="color: #0000ff;" class="mycode_color">void</span> f(<span style="color: #0000ff;" class="mycode_color">int</span> i)<br />
{<br />
    <span style="color: #0000ff;" class="mycode_color">while</span>(i) <span style="color: #0000ff;" class="mycode_color">try</span> {             <span style="color: #008000;" class="mycode_color">// Non-compliant</span><br />
        <span style="color: #008000;" class="mycode_color">//...</span><br />
    } <span style="color: #0000ff;" class="mycode_color">catch</span>(Exception e){<br />
    }<br />
}</span></span><br />
<br />
I think this is non-compliant with the Rule 9.3.1, because 'try' is not a compound statement. However, accordingly to the standard 'try' should be always followed by a compound statement if it is inside a function. This code seems to be clear, so maybe this case could be added as an exception in the future versions of MISRA C++ standard.]]></description>
			<content:encoded><![CDATA[It is possible to add the 'try' statement directly after 'while', for example:<br />
<br />
<span style="color: #000000;" class="mycode_color"><span style="font-family: Consolas, 'Liberation Mono', Courier, monospace, Consolas, 'Courier New', monospace;" class="mycode_font"><span style="color: #0000ff;" class="mycode_color">class</span> Exception{};<br />
<span style="color: #0000ff;" class="mycode_color">void</span> f(<span style="color: #0000ff;" class="mycode_color">int</span> i)<br />
{<br />
    <span style="color: #0000ff;" class="mycode_color">while</span>(i) <span style="color: #0000ff;" class="mycode_color">try</span> {             <span style="color: #008000;" class="mycode_color">// Non-compliant</span><br />
        <span style="color: #008000;" class="mycode_color">//...</span><br />
    } <span style="color: #0000ff;" class="mycode_color">catch</span>(Exception e){<br />
    }<br />
}</span></span><br />
<br />
I think this is non-compliant with the Rule 9.3.1, because 'try' is not a compound statement. However, accordingly to the standard 'try' should be always followed by a compound statement if it is inside a function. This code seems to be clear, so maybe this case could be added as an exception in the future versions of MISRA C++ standard.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[9.4.2 - Why C-style enums are exempted from requiring a default clause?]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1699</link>
			<pubDate>Thu, 22 Aug 2024 18:03:16 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5614">cgpzs</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1699</guid>
			<description><![CDATA[Rule 9.4.2 requires that all switch blocks have a "default" case, for defensive programming.<br />
<br />
This is however not required when using C-style enums, i.e. unscoped and without underlying type. Presumably because it's Undefined Behavior to cast an int out-of-range to a C-style enum, whereas it's well-defined behavior for "C++ enums". Since UB is assumed to not happen, one can assume that a C-style enum has a valid value.<br />
<br />
However, there's a subtlety. The "range" of a C-style enum is not just the list of valid enumerators, but the smallest bitset that contains all enumerators.<br />
<br />
So for example:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>enum Foo<br />
{<br />
&nbsp;&nbsp; a = 0,<br />
&nbsp;&nbsp; b = 1,<br />
&nbsp;&nbsp; c = 2,<br />
&nbsp;&nbsp; d = 3<br />
};</code></div></div><br />
The following code is **not** Undefined Behavior, because the valid range of this enum is 0-7 (2^3 - 1).<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>enum Foo x = static_cast&lt;Foo&gt;(7);</code></div></div><br />
So I'd argue that defensive programming still applies here and a "default" case would still be needed.<br />
<br />
What do you think?]]></description>
			<content:encoded><![CDATA[Rule 9.4.2 requires that all switch blocks have a "default" case, for defensive programming.<br />
<br />
This is however not required when using C-style enums, i.e. unscoped and without underlying type. Presumably because it's Undefined Behavior to cast an int out-of-range to a C-style enum, whereas it's well-defined behavior for "C++ enums". Since UB is assumed to not happen, one can assume that a C-style enum has a valid value.<br />
<br />
However, there's a subtlety. The "range" of a C-style enum is not just the list of valid enumerators, but the smallest bitset that contains all enumerators.<br />
<br />
So for example:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>enum Foo<br />
{<br />
&nbsp;&nbsp; a = 0,<br />
&nbsp;&nbsp; b = 1,<br />
&nbsp;&nbsp; c = 2,<br />
&nbsp;&nbsp; d = 3<br />
};</code></div></div><br />
The following code is **not** Undefined Behavior, because the valid range of this enum is 0-7 (2^3 - 1).<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>enum Foo x = static_cast&lt;Foo&gt;(7);</code></div></div><br />
So I'd argue that defensive programming still applies here and a "default" case would still be needed.<br />
<br />
What do you think?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[MISRA C++ 9.6.2]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1695</link>
			<pubDate>Sat, 06 Jul 2024 02:20:54 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=1246">gdavis</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1695</guid>
			<description><![CDATA[Hello,<br />
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".<br />
<br />
<span style="font-family: Courier New;" class="mycode_font">void f3(int32_t i, int32_t x, int32_t y)</span><br />
<span style="font-family: Courier New;" class="mycode_font">{</span><br />
<span style="font-family: Courier New;" class="mycode_font">  switch( i )</span><br />
<span style="font-family: Courier New;" class="mycode_font">  {                                   // statement A</span><br />
<span style="font-family: Courier New;" class="mycode_font">    case 0:</span><br />
<span style="font-family: Courier New;" class="mycode_font">      if ( x &lt; y )</span><br />
<span style="font-family: Courier New;" class="mycode_font">        goto L3;                      // Non-compliant</span><br />
<span style="font-family: Courier New;" class="mycode_font">      break;</span><br />
<span style="font-family: Courier New;" class="mycode_font">    case 1:</span><br />
<span style="font-family: Courier New;" class="mycode_font">    L3:</span><br />
<span style="font-family: Courier New;" class="mycode_font">      break;</span><br />
<span style="font-family: Courier New;" class="mycode_font">  }</span><br />
<span style="font-family: Courier New;" class="mycode_font">}</span><br />
<br />
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).<br />
<br />
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"<br />
<br />
Please let me know what you think. Thank you.]]></description>
			<content:encoded><![CDATA[Hello,<br />
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".<br />
<br />
<span style="font-family: Courier New;" class="mycode_font">void f3(int32_t i, int32_t x, int32_t y)</span><br />
<span style="font-family: Courier New;" class="mycode_font">{</span><br />
<span style="font-family: Courier New;" class="mycode_font">  switch( i )</span><br />
<span style="font-family: Courier New;" class="mycode_font">  {                                   // statement A</span><br />
<span style="font-family: Courier New;" class="mycode_font">    case 0:</span><br />
<span style="font-family: Courier New;" class="mycode_font">      if ( x &lt; y )</span><br />
<span style="font-family: Courier New;" class="mycode_font">        goto L3;                      // Non-compliant</span><br />
<span style="font-family: Courier New;" class="mycode_font">      break;</span><br />
<span style="font-family: Courier New;" class="mycode_font">    case 1:</span><br />
<span style="font-family: Courier New;" class="mycode_font">    L3:</span><br />
<span style="font-family: Courier New;" class="mycode_font">      break;</span><br />
<span style="font-family: Courier New;" class="mycode_font">  }</span><br />
<span style="font-family: Courier New;" class="mycode_font">}</span><br />
<br />
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).<br />
<br />
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"<br />
<br />
Please let me know what you think. Thank you.]]></content:encoded>
		</item>
	</channel>
</rss>