Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

According to the C Standard, 6.8.5.3, paragraph 4 [ISO/IEC 9899:2024],

A switch statement causes control to jump to, into, or past the statement that is the switch body, depending on the value of a controlling expression, and on the presence of a default label and the values of any case labels on or in the switch body. A case or default label is accessible only within the closest enclosing switch statement

...

.

If a programmer declares variables, initializes them before the first case statement, and then tries to use them inside any of the case statements, those variables will have scope inside the switch block but will not be initialized and will consequently contain garbage values.indeterminate values.  Reading such values also violates EXP33-C. Do not read uninitialized memory.

Noncompliant Code Example

This noncompliant code example declares variables and contains executable statements before the first case label within the switch statement.:

Code Block
bgColor#FFCCCC
langc
#include <stdio.h>
 
extern void f(int i);
 
void func(int expr) {
  switch (expr) {
    int i = 4;
    f(i);
  case 0:
    i = 17;
    /* fallsFalls through into default code */
  default:
    printf(“%d"%d\n”n", i);
  }
  return 0;
}

Implementation Details

When the above the preceding example is executed on GCC Version 4.8.1.2, the variable i is instantiated with automatic storage duration within the block, but it is not initialized. Consequently, if the controlling expression expr has a non-zero nonzero value, the call to printf() will access an indeterminate value of i. Similarly, the call to f() is not executed.

i

Value of expr


Output

0

17

non-zero

Nonzero

indeterminate

Indeterminate

Compliant Solution

In this compliant solution, the statements before the first case label occur before the switch statement, improving the predictability and readability of the code.:

Code Block
bgColor#ccccff
langc
#include <stdio.h>
 
extern void f(int i);
 
int func(int expr) {
  int/*
 i = 4;    /* Move the code outside the switch block; */
now  f(i);the statements
   * will get executed.
    */*
 Now theint statementsi will= get4;
 executed */ f(i);

  switch (expr) {
    case 0:
      i = 17;
      /*falls Falls through into default code */
    default:
      printf(“%d"%d\n”n", i);
  }
  return 0;
}

Risk Assessment

Using test conditions or initializing variables before the first case statement in a switch block can result in unexpected behavior because that code will not be executed and undefined behavior 20.

Recommendation

Rule

Severity

Likelihood

Detectable

Remediation Cost

Repairable

Priority

Level

MSC35-C

medium

unlikely

medium

P4

L3

Related Guidelines

MISRA Chapter 6.14

DCL41-C

Medium

Unlikely

Yes

Yes

P6

L2

Automated Detection

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V

switch-skipped-code

Fully checked
Axivion Bauhaus Suite
Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V
CertC-DCL41Fully implemented
Clang
Include Page
Clang_V
Clang_V
-Wsometimes-uninitialized

CodeSonar
Include Page
CodeSonar_V
CodeSonar_V
LANG.STRUCT.SW.BADMalformed switch Statement
Coverity
Include Page
Coverity_V
Coverity_V

MISRA C 2004 Rule 15.0

MISRA C 2012 Rule 16.1

Implemented
Cppcheck Premium
Include Page
Cppcheck Premium_V
Cppcheck Premium_V

premium-cert-dcl41-c


Helix QAC
Include Page
Helix QAC_V
Helix QAC_V
C2008, C2882, C3234Fully implemented
Klocwork
Include Page
Klocwork_V
Klocwork_V
CERT.DCL.SWITCH.VAR_BEFORE_CASEFully implemented
LDRA tool suite 
Include Page
LDRA_V
LDRA_V
385 SFully implemented
Parasoft C/C++test
Include Page
Parasoft_V
Parasoft_V
CERT_C-DCL41-a

A switch statement shall only contain switch labels and switch clauses, and no other code

PC-lint Plus

Include Page
PC-lint Plus_V
PC-lint Plus_V

527

Assistance provided

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C: Rule DCL41-C

Checks for ill-formed switch statements (rule partially covered)

PVS-Studio

Include Page
PVS-Studio_V
PVS-Studio_V

V622
RuleChecker

Include Page
RuleChecker_V
RuleChecker_V

switch-skipped-code

Fully checked
TrustInSoft Analyzer

Include Page
TrustInSoft Analyzer_V
TrustInSoft Analyzer_V

initialisationExhaustively detects undefined behavior (see the compliant and the non-compliant example).

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

Key here (explains table format and definitions)

Taxonomy

Taxonomy item

Relationship

MISRA C:2012Rule 16.1 (required)Prior to 2018-01-12: CERT: Unspecified Relationship

Bibliography

[ISO/IEC 9899:2024]6.8.5.3, "The switch Statement"

...


...

Image Modified Image Modified Image Modified