You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 95 Next »

The presence of unused values may indicate significant logic errors. To prevent such errors, unused values should be identified and removed from code.

This recommendation is a specific case of MSC12-C. Detect and remove code that has no effect or is never executed.

Noncompliant Code Example

In this example, p2 is assigned the value returned by bar(), but that value is never used. Note this example assumes that foo() and bar() return valid pointers (see DCL30-C. Declare objects with appropriate storage durations).

int *p1;
int *p2;
p1 = foo();
p2 = bar();

if (baz()) {
  return p1;
}
else {
  p2 = p1;
}
return p2;

Compliant Solution

This example can be corrected in many different ways, depending on the intent of the programmer. In this compliant solution, p2 is found to be extraneous. The calls to bar() and baz() can be removed if they do not produce any side effects.

int *p1 = foo();

/* Removable if bar() does not produce any side effects */
(void)bar();

/* Removable if baz() does not produce any side effects */
(void)baz();
return p1;

Risk Assessment

Unused values may indicate significant logic errors.

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

MSC13-C

Low

Unlikely

Medium

P2

L3

Automated Detection

Tool

Version

Checker

Description

Astrée24.04 Supported, but no explicit checker
CodeSonar8.1p0

LANG.STRUCT.UUVAL

Unused value

Coverity

2017.07

UNUSED_VALUE

Finds variables that are assigned pointer values returned from a function call but never used

Klocwork2024.3

LV_UNUSED.GEN
VA_UNUSED.GEN
VA_UNUSED.INIT

 

LDRA tool suite9.7.1

1 D, 8 D, 105 D, 94 D, 15 D

Fully implemented

Parasoft C/C++test 2023.1 MISRA2008-0_1_3_bFully implemented
Polyspace Bug FinderR2016a

Unused parameter

Write without a further read

Function prototype has parameters not read or written in function body

Variable never read after assignment

PRQA QA-C
Unable to render {include} The included page could not be found.

1500, 1502, 3203, 3205, 3206, 3207, 3229, 3196, 2980, 2981, 2982, 2983, 2984, 2985, 2986

Fully implemented
SonarQube C/C++ Plugin3.11S1854 

Related Vulnerabilities

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

Related Guidelines

SEI CERT C++ Coding StandardVOID MSC13-CPP. Detect and remove unused values
ISO/IEC TR 24772Likely Incorrect Expressions [KOA]
Dead and Deactivated Code [XYQ]
Unused Variable [XYR]

Bibliography

 


  • No labels