Main Content

Code deactivated by constant false condition

Code segment deactivated by #if 0 directive or if(0) condition

Description

This defect occurs when a block of code is deactivated using a #if 0 directive or if(0) condition.

Risk

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.

Fix

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:

Examples

expand all

#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
    /* 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.

Correction — Change #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;
}

In this example, the condition (do_it && i) is always false if the template ATemplateClass is instantiated using a false Boolean value.

// Corner case with templates
#include <iostream>

template<bool do_it>
class ATemplateClass {
public:
	void foo(int i) {
		if(do_it && i) {  // Defect
			std::cout << "Code enters if branch of foo().\n";
		}
	}
};
class AlwaysTrue: public ATemplateClass<true> {};
class AlwaysFalse: public ATemplateClass<false> {};

void defect_raised_with_multiple_template_instances() {
	AlwaysTrue t;
	AlwaysFalse f;
	t.foo(1);
	f.foo(1);
}

The preceding code specifies the Boolean value as false for the derived class AlwaysFalse. Because the condition is defined as part of the template, not all instantiation of the template results in a defect. The defect checker reports a defect if at least one instantiation results in a defect.

Result Information

Group: Data flow
Language: C | C++
Default: off
Command-Line Syntax: DEACTIVATED_CODE
Impact: Low

Version History

Introduced in R2013b