Code that is executed but does not perform any action, or has an unintended effect, most likely results from a coding error and can result in unexpected behavior and vulnerabilities. Statements or expressions that have no effect should be identified and removed from code.
Non-Compliant Code Example
...
(assignment)
In this example, the comparison of a
to b
has no effect.
...
This is likely a case of the programmer mistakenly using the equals operator ==
instead of the assignment operator =
.
Compliant Solution
...
(assignment)
The assignment of b
to a
is now properly performed.
Code Block | ||
---|---|---|
| ||
int a; int b; /* ... */ a = b; |
Non-Compliant Code Example
...
(dereference)
In this example, p
is incremented and then dereferenced. However, *p
has no effect.
Code Block | ||
---|---|---|
| ||
int *p; /* ... */ *p++; |
Compliant Solution
...
(dereference)
Correcting this example depends on the intent of the programmer. For instance, if dereferencing p
was done on accident, then p
should not be dereferenced.
...