[previous] 2882 [next] This 'switch' statement will bypass the initialization of local variables.
Control flow

A local object with automatic storage duration is being defined with an initializer before the first case label in a switch statement. Storage space for the object will be reserved but the initialization will not be performed. The value of this object will be indeterminate and if it is used, the result will be undefined.

For example:


/*PRQA S 0689,2017,2120,3120,3201,3220,3227,3234,3321,3408,3447 ++*/

extern void func(int p);

extern void foo(int n)
{
    switch (n)                      /* 2882 */
    {
        int x = 3;
    case 1:
        func(x + 1);                /* 2961 */
        break;

    case 2:
        func(x + n);
        break;

    default:
        x = 5;
        func(x + n);
        break;
    }
}


MISRA-C:2004 Rules applicable to message 2882:

Rule  14.1  (Required) There shall be no unreachable code.


QA·C Source Code Analyser 8.1
MISRA-C:2004 Compliance Module 3.2
© 2012 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents