CWE Rule 570
Description
Rule Description
The software contains an expression that will always evaluate to false.
Polyspace Implementation
The rule checker checks for these issues:
Code deactivated by constant false condition
Dead code
Examples
Code deactivated by constant false condition
This issue occurs
when a block of code is deactivated using a #if 0
directive
or if(0)
condition.
A #if 0
directive or if(0)
condition is used
to temporarily deactivate segments of code. If your production code contains these
directives, it means that the deactivation has not been lifted before shipping the
code.
If the segment of code is present for debugging purposes only, remove the segment
from production code. If the deactivation occurred by accident, remove the
#if 0
and #endif
statements.
Often, a segment of code is deactivated for specific conditions, for instance, a
specific operating system. Use macros with the #if
directive to
indicate these conditions instead of deactivating the code completely with a
#if 0
directive. For instance, GCC provides macros to detect
the Windows® operating
system:
#ifdef _WIN32 //Code deactivated for all operating systems //Other than 32-bit Windows #endif
If you do not want to fix the issue, add comments to your result or code to avoid another review. See:
Address Results in Polyspace User Interface Through Bug Fixes or Justifications if you review results in the Polyspace user interface.
Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access) if you review results in a web browser.
Annotate Code and Hide Known or Acceptable Results if you review results in an IDE.
#include<stdio.h> int Trim_Value(int* Arr,int Size,int Cutoff) { int Count=0; for(int i=0;i < Size;i++){ if(Arr[i]>Cutoff){ Arr[i]=Cutoff; Count++; } } #if 0 //Noncompliant /* Defect: Code Segment Deactivated */ if(Count==0){ printf("Values less than cutoff."); } #endif return Count; }
In the preceding code, the printf
statement
is placed within a #if #endif
directive. The software
treats the portion within the directive as code comments and not compiled.
#if 0
to #if
1
Unless you intended to deactivate the printf
statement,
one possible correction is to reactivate the block of code in the #if
#endif
directive. To reactivate the block, change #if
0
to #if 1
.
#include<stdio.h> int Trim_Value(int* Arr,int Size,int Cutoff) { int Count=0; for(int i=0;i < Size;i++) { if(Arr[i]>Cutoff) { Arr[i]=Cutoff; Count++; } } /* Fix: Replace #if 0 by #if 1 */ #if 1 if(Count==0) { printf("Values less than cutoff."); } #endif return Count; }
Dead code
This issue occurs when a block of code cannot be reached because of a condition that is always true or false. This defect excludes:
Code deactivated by constant false condition
, which checks for directives with compile-time constants such as#if 0
orif(0)
.Unreachable code
, which checks for code after a control escape such asgoto
,break
, orreturn
.Useless if
, which checks for if statements that are always true.
Dead code wastes development time, memory and execution cycles. Developers have to maintain code that is not being executed. Instructions that are not executed still have to be stored and cached.
Dead code often represents legacy code that is no longer used. Cleaning up dead code periodically reduces future maintenance.
The fix depends on the root cause of the defect. For instance, the root cause can be an error condition that is checked twice on the same execution path, making the second check redundant and the corresponding block dead code.
Often the result details (or source code tooltips in Polyspace as You Code) show a sequence of events that led to the defect. You can implement the fix on any event in the sequence. If the result details do not show this event history, you can search for previous references of variables relevant to the defect using right-click options in the source code and find related events. See also Interpret Bug Finder Results in Polyspace Desktop User Interface or Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access).
See examples of fixes below.
If you see dead code from use of functions such as isinf
and
isnan
, enable an analysis mode that takes into account
non-finite values. See Consider non
finite floats (-allow-non-finite-floats)
.
If you do not want to fix the issue, add comments to your result or code to avoid another review. See:
Address Results in Polyspace User Interface Through Bug Fixes or Justifications if you review results in the Polyspace user interface.
Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access) if you review results in a web browser.
Annotate Code and Hide Known or Acceptable Results if you review results in an IDE.
#include <stdio.h> int Return_From_Table(int ch){ int table[5]; /* Create a table */ for(int i=0;i<=4;i++){ table[i]=i^2+i+1; } if(table[ch]>100){ //Noncompliant return 0; } return table[ch]; }
The maximum value in the array table
is 4^2+4+1=21
,
so the test expression table[ch]>100
always
evaluates to false. The return 0
in the if
statement
is not executed.
One possible correction is to remove the if
condition
from the code.
#include <stdio.h> int Return_From_Table(int ch){ int table[5]; /* Create a table */ for(int i=0;i<=4;i++){ table[i]=i^2+i+1; } return table[ch]; }
typedef enum _suit {UNKNOWN_SUIT, SPADES, HEARTS, DIAMONDS, CLUBS} suit; suit nextcard(void); void do_something(suit s); void bridge(void) { suit card = nextcard(); if ((card < SPADES) || (card > CLUBS)) card = UNKNOWN_SUIT; if (card > 7) { //Noncompliant do_something(card); } }
The type suit
is enumerated
with five options. However, the conditional expression card
> 7
always evaluates to
false because card
can be at most 5. The content
in the if
statement is not executed.
One possible correction is to change the if-condition
in the code. In this correction, the 7 is changed to HEART
to
relate directly to the type of card
.
typedef enum _suit {UNKNOWN_SUIT, SPADES, HEARTS, DIAMONDS, CLUBS} suit; suit nextcard(void); void do_something(suit s); void bridge(void) { suit card = nextcard(); if ((card < SPADES) || (card > CLUBS)) card = UNKNOWN_SUIT; if (card > HEARTS) { do_something(card); } }
Check Information
Category: Expression Issues |
Version History
Introduced in R2023a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)