You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Do not use the bitwise AND (&, ampersand) or bitwise OR (|, pipe) operator in a conditional expression because this typically indicates programmer error and can result in unexpected behavior. Only use & or | only for bitwise operations, and to use && or || only for logical operations.

Noncompliant Code Example

In this noncompliant code example, a bitwise expression is used in a conditional expression.

if (!(getuid() & geteuid() == 0)) { 
  /* ... */ 
} 

Compliant Solution

This compliant solution uses the && operator for the logical operation within the conditional expression.

if (!(getuid() && geteuid() == 0)) {
  /* ... */
}

Bibliography

[Hatton 95] Section 2.7.2, "Errors of omission and addition"
[ISO/IEC PDTR 24772] "KOA Likely Incorrect Expressions"
[Seacord 09] "MSC02-C. Avoid errors of omission"

  • No labels