CXX.LOGICAL_OP.INT_OPERAND

Potential logic error: Integral expressions should not be used directly as logical operator operands. Boolean expressions should be used instead.

This checker reports defects when a logical operator is applied directly to Integral expressions.

Vulnerability and risk

Performing logical operators on Integral expressions can lead to unexpected results.

Vulnerable code example

Copy
   bool func(int var1, int var2)
   {
   const bool var = false;
   const int Constant1 = true;
   const int Constant2 = true;
   return var == Constant1 || Constant2;  
   }

In the above example, line 6 is noncompliant because a logical operator is applied to the Integral expression. Klocwork produces a CXX.LOGICAL_OP.INT_OPERAND defect at line 6, indicating, “Potential logic error: Integral expressions should not be used directly as logical operator operands. Boolean expressions should be used instead.”

Fixed code example

Copy
   bool func(int var1, int var2)
   {
   const bool var = false;
   const int Constant1 = true;
   const int Constant2 = true;
   return var == Constant1 || var == Constant2;  
   }

In the fixed example, Klocwork no longer reports the defect CXX.LOGICAL_OP.INT_OPERAND at line 6.