...
Code Block | ||||
---|---|---|---|---|
| ||||
volatile int *p; /* ... */ (void) *(p++); |
Noncompliant Code Example (if/else if)
A chain of if/else if statements is evaluated from top to bottom. At most, only one branch of the chain will be executed: the first one with a condition that evaluates to true. Consequently, duplicating a condition in a sequence of if/else if statements automatically leads to dead code.
Code Block | ||||
---|---|---|---|---|
| ||||
if (param == 1)
openWindow();
else if (param == 2)
closeWindow();
else if (param == 1) /* Duplicated condition */
moveWindowToTheBackground();
|
Compliant Solution (if/else if)
In this compliant solution, the third conditional expression has been corrected.
Code Block | ||||
---|---|---|---|---|
| ||||
if (param == 1)
openWindow();
else if (param == 2)
closeWindow();
else if (param == 3)
moveWindowToTheBackground();
|
Risk Assessment
The presence of code that has no effect can indicate logic errors that may result in unexpected behavior and vulnerabilities.
...
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
CodeSonar |
| LANG.STRUCT.EBS LANG.STRUCT.RC MISC.NOEFFECT | Empty {Branch, for, if, switch, while} Statement Redundant Condition Funcion Call Has No Effect | ||||||
| NO_EFFECT | Finds statements or expressions that do not accomplish anything or statements that perform an unintended action | |||||||
| CC2.MSC12 | Partially implemented | |||||||
3.0 | Options detect unused local variables or nonconstant static variables and unused function parameters, respectively | ||||||||
| EFFECT |
| |||||||
| 65 D | Fully implemented | |||||||
PRQA QA-C |
| 3426,3427,3307,3110,3112,3404 | Partially implemented | ||||||
SonarQube |
| S1862 | |||||||
|
|
|
...
Bibliography
...