Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: added example from c:S1764

...

Correcting this example depends on the intent of the programmer. For instanceexample, if dereferencing p was a mistake, then p should not be dereferenced.

...

Code Block
bgColor#ccccff
langc
if (param == 1)
   openWindow();
 else if (param == 2)
   closeWindow();
 else if (param == 3)
   moveWindowToTheBackground();

Noncompliant Code Example (logical operators)

Using the same subexpression on either side of a logical operator is almost always a mistake.  In this noncompliant code example, the rightmost subexpression of the controlling expression of each if statement has no effect.  

Code Block
bgColor#FFCCCC
langc
if (a == b && a == b) { // if the first one is true, the second one is too
  do_x();
}
if (a == c || a == c ) { // if the first one is true, the second one is too
  do_w();
}

Compliant Solution (logical operators)

In this compliant solution, the rightmost subexpression of the controlling expression of each if statement has been removed.

Code Block
bgColor#ccccff
langc
if (a == b) { 
  do_x();
}
if (a == c) { 
  do_w();
}

Risk Assessment

The presence of code that has no effect can indicate logic errors that may result in unexpected behavior and vulnerabilities.

...