Skip to main content

IAR Embedded Workbench for RX 5.20

RED-cond-const-assign

In this section:
Synopsis

A constant assignment in a conditional expression.

Enabled by default

Yes

Severity/Certainty

Low/Medium

lowmedium.png
Full description

An assignment of a constant to a variable is used in a conditional expression. It is most likely an accidental use of the assignment operator (=) instead of the comparison operator (==). The usual result of an assignment operation is the value of the right-hand operand, which in this case is a constant value. This constant value is being compared to zero in the condition, then an execution path is chosen. Any alternate paths are unreachable because of this constant condition.

Coding standards
CWE 481

Assigning instead of Comparing

CWE 570

Expression is Always False

CWE 571

Expression is Always True

Code examples

The following code example fails the check and will give a warning:

int * foo(int* y, int size){
  int counter = 100;
  int * orig = y;
  while (y = 0) {
    if (counter)
      continue;
    else
      return orig;

  };
}

The following code example passes the check and will not give a warning about this issue:

int * foo(int* y, int size){
  int counter = 100;
  int * orig = y;
  while (*y++ = 0) {
    if (++counter)
      continue;
    else
      return orig;

  };
}