Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

INVARIANT_CONDITION.UNREACH

Invariant expression in a condition, which causes unreachable code

The INVARIANT_CONDITION.UNREACH checker searches for true/false conditions in selection or iteration statements that cause unreachable code issues in their branches. This differs from unreachable code checkers, such as UNREACH.GEN or UNREACH.RETURN which report the unreachable code itself regardless of what caused it. The INVARIANT_CONDITION.UNREACH checker reports only the condition which caused an unreachable code. Since these checkers detect similar issues (though in a slightly different way) it makes sense to turn on INVARIANT_CONDITION.UNREACH only when unreachable code checkers are turned off.

Vulnerability and risk

Invariant conditions can cause unintended program behavior due to a mismatch between the code as written and the intended design. Invariant conditions can also cause confusion during code maintenance or code review.

Code example

  1 enum status_codes {
  2     ST_UNKNOWN = -1,
  3     ST_SUCCESS,
  4     ST_INVALIDARG,
  5     ST_NOTFOUND,
  6     ST_FATAL,
  7 };
  8
  9 extern void report_warn();
 10 extern void report_err();
 11
 12
 13 void report_status(int code)
 14 {
 15     if (code < 0) {
 16         exit(1);
 17     }
 18     if (code >= ST_FATAL) {
 19         report_err();
 20     } else {
 21         report_warn();
 22         if (code == ST_UNKNOWN) {  //   <== invariant condition
 23             exit(2);               // ( <== unreachable code )
 24         }
 25     }
 26 }

Klocwork reports the 'code == ST_UNKNOWN' condition at line 22 as an INVARIANT_CONDITION.UNREACH issue, since the value of the variable 'code' is guarded by a non-negative value at this path by a previous check at line 15. The code branch controlled by this condition at line 13 is unreachable.

To fix this issue, change the order in which the conditions are checked.