Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Implicit type conversions in switch statements
#1
I've a question concerning the following example:
Code:
int8 x;
...
switch(x)
{
    case 0:      /* valid */
    case 255:    /* valid? */
    case 1L:     /* invalid, implicit cast from long to int */
        ...
}

I would expect any good static checker to report that x can never equal 255 here. But is 'case 255:' actually a MISRA 2004 rule violation? The C standard says that the switch expression x gets promoted to 'int' in this case, so the type of the case label 255 does match the type of the promoted switch expression. I can't find any MISRA rule that says that we should consider the underlying type (instead of the promoted type) of the switch variable when deciding whether a case label is allowed or not. If "case 255:" was the only case label on some code, then rule 14.1 (no unreachable code) would apply; but in this example, there is no unreachable code.
<r>David Crocker<br/>
Escher Technologies Ltd.<br/>
<URL url="http://www.eschertech.com">http://www.eschertech.com</URL></r>
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)