Switches, default, and enums - Printable Version +- MISRA Discussion Forums (https://forum.misra.org.uk) +-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4) +--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17) +---- Forum: 6.15 Switch Statements (https://forum.misra.org.uk/forumdisplay.php?fid=45) +---- Thread: Switches, default, and enums (/showthread.php?tid=362) |
Switches, default, and enums - ChrisNelson - 24-05-2007 Rule 15.3 requires every switch to end with a default clause. While this is usually a good idea, it undermines the technique of using enums for switch control and having the compiler catch unused or unhandled values. For example: Code: typedef enum { With that in mind, I suggest that rule 15.3 be revised by adding, \"except where the switch expression is an enum\" to the end. I might also add a advisory rule encouraging switch expressions to be enums. Any thoughts? Re: Switches, default, and enums - sparker - 25-05-2007 ChrisNelson Wrote:Rule 15.3 requires every switch to end with a default clause. [...] For example: While I have some sympathy with this position, we have to realise that enums are just ints beneath, and there's nothing much to stop you writing Code: f((color_t) -1); so we have to balance the static checking offered by the lint against the dynamic checking available through a suitable default case. One possibility might be to hide the default case from the lint; although this would bring up another set of issues needing justification. As usual, where there is some debate in the issue, it is probably best if you raise a deviation to the rule containing your proviso ("except where the switch expression is an enum") with appropriate justification. stephen Re: Switches, default, and enums - lv - 06-06-2007 ChrisNelson Wrote:Many compilers and certainly most lints...Then it cannot be a MISRA rules because it is not "all compiler", and this is not applicable for all compiler because this behavior (generat a warning in this case) is unspecified by the ISO 9899. That could only be a project/company deviation. - misra-c - 11-12-2007 sparker's reply is consistant with the view of the working group. Re: Switches, default, and enums - Peter Obermayer - 09-01-2008 Do I understand correctly that even if the cases of a switch statement exhaust all elements of an enum the default statement is not \"unreachable\" in the sense of MISRA-2004:14.1 / MISRA 98:52 due to the argument given by sparker? - George Brown - 09-01-2008 When writing code for safety-critical systems, I have always considered treating all RAM locations to be \"volatile\" in nature. If I have code which can set a 16 bit unsigned integer (represeting an enum) explicitly to the values of 0, 1, 2, and 3, I also always handle the cases of it being any other value the storage may select, i.e. 4 to 65535. Given Code: typedef enum { Treat color to be volatile, and assume that a failure mechanisum WILL exist to randomly set it. Given this, the need for the default clause is not in question. (imho) George - Lundin - 14-01-2008 I agree completely with George, an important purpose of the rule is defensive programming against \"RAM noise\", ie in a safety-critical system the RAM can't be trusted to keep its values during long periods of time. Also, if there is an undetected runtime bug altering a memory location unexpectedly, the bug can be detected, and damage caused by it avoided, if using this method. The same argument can be used for justifying rule 14.10 saying that you must end an \"if...else if\" with \"else\". |