CodeQL documentation

Bad check for oddness

ID: cpp/incomplete-parity-check
Kind: problem
Security severity: 
Severity: warning
Precision: medium
Tags:
   - reliability
   - correctness
   - types
Query suites:
   - cpp-security-and-quality.qls

Click to see the query in the CodeQL repository

This rule finds code that uses x % 2 == 1 to check whether a number x is odd, which does not work for negative numbers. Applying % to negative numbers produces negative results. For example, (-5) % 2 equals -1, not 1. As a result, this check incorrectly considers all negative numbers as even.

Recommendation

Consider using x % 2 != 0 or (x & 1) == 1 instead.

References

  • © GitHub, Inc.
  • Terms
  • Privacy
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy