ASSIGCOND.GEN

Assignment in conditional expression

The ASSIGCOND.GEN checker finds conditional statements that include an assignment expression.

Vulnerability and risk

This checker typically finds syntax errors, usually cases in which an assignment operator is used mistakenly instead of a comparison operator. If the error isn't corrected, unintended program behavior is likely to occur.

Vulnerable code example

Copy
  class A{
     void foo();
  };
  void A::foo()
  {
      int i = 1;
      int j = 0;
      if(i = j) j++; 
  }

In the code example, Klocwork has flagged line 8 because the if statement appears to include an assignment.

Fixed code example 1

Copy
  class A{
     void foo();
  };
  void A::foo()
  {
      int i = 1;
      int j = 0;
      if((i == j)) j++; 
  }

In this fixed code, the assignment operator has been replaced with the intended comparison operator.

Fixed code example 2

Copy
  class A{
     void foo();
  };
  void A::foo()
  {
      int i = 1;
      int j = 0;
      if((i=qq()) !=0) j++; 
  }

In this fixed example, brackets have been used to make the assignment syntax clear.

Related checkers

Security training

Application security training materials provided by Secure Code Warrior.