Versions Compared

Key

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

...

When the preceding example is executed on GCC 4.8.1, 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 nonzero value, the call to printf() will access an indeterminate value of i. Similarly, the call to f() is not executed.

Value of expr

Output

0

17

Nonzero

Indeterminate

Compliant Solution

In this compliant solution, the statements before the first case label occur before the switch statement:

...

Using test conditions or initializing variables before the first case statement in a switch block can result in unexpected behavior and undefined behavior.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

DCL41-C

Medium

Unlikely

Medium

P4

L3

Automated Detection

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V

switch-skipped-code

Fully checked
Clang
Include Page
Clang_V
Clang_V
-Wsometimes-uninitialized
 

Coverity
Include Page
Coverity_V
Coverity_V

MISRA C 2004 Rule 15.0

MISRA C 2012 Rule 16.1

Implemented
LDRA tool suite 
Include Page
LDRA_V
LDRA_V
385 SFully implemented
Parasoft C/C++test
Include Page
c:
Parasoft_V
c:
Parasoft_V
MISRA2004-15_0_bFully implemented
Astrée
Include Page
Astrée_V
Astrée_V

future-library-use

language-override

language-override-c99

reserved-declaration

reserved-declaration-c99

reserved-identifier

Partially checked
PRQA QA-C
Include Page
PRQA QA-C_v
PRQA QA-C_v

3234
2008
2882

Partially implemented

RuleChecker
Include Page
RuleChecker_V
RuleChecker_V

switch-skipped-code

Fully checked
PVS-Studio6.22V622General analysis rule set

Related Vulnerabilities

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

...

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:2011]6.8.4.2, "The switch Statement"

...


...