52 lines
1.1 KiB
Plaintext
52 lines
1.1 KiB
Plaintext
![]() |
Calculating or retrieving a value only to then overwrite it or throw it away, could indicate a serious error in the code. Even if it's not an error, it is at best a waste of resources. Therefore all calculated values should be used.
|
||
|
|
||
|
|
||
|
== Noncompliant Code Example
|
||
|
|
||
|
----
|
||
|
void calculateRate(int a, int b)
|
||
|
{
|
||
|
int i;
|
||
|
|
||
|
i = a + b; // Noncompliant; calculation result not used before value is overwritten
|
||
|
i = doSomething(); // Noncompliant; retrieved value not used
|
||
|
for (i = 0; i < 10; i++) {
|
||
|
// ...
|
||
|
}
|
||
|
// ...
|
||
|
}
|
||
|
----
|
||
|
|
||
|
|
||
|
== Compliant Solution
|
||
|
|
||
|
----
|
||
|
void calculateRate(int a, int b)
|
||
|
{
|
||
|
int i;
|
||
|
|
||
|
i = doSomething();
|
||
|
i += a + b;
|
||
|
storeI(i)
|
||
|
|
||
|
for (i = 0; i < 10; i++) {
|
||
|
// ...
|
||
|
}
|
||
|
}
|
||
|
----
|
||
|
|
||
|
|
||
|
== See
|
||
|
|
||
|
* https://www.securecoding.cert.org/confluence/x/QYA5[CERT, MSC13-C] - Detect and remove unused values
|
||
|
* https://www.securecoding.cert.org/confluence/x/S4IyAQ[CERT, MSC13-CPP] - Detect and remove unused values
|
||
|
|
||
|
|
||
|
|
||
|
ifdef::env-github,rspecator-view[]
|
||
|
== Comments And Links
|
||
|
(visible only on this page)
|
||
|
|
||
|
include::comments-and-links.adoc[]
|
||
|
endif::env-github,rspecator-view[]
|