Bad check for oddness¶
ID: cpp/incomplete-parity-check Kind: problem Severity: warning Precision: medium Tags: - reliability - correctness - types Query suites: - cpp-security-and-quality.qls
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. As a result, this check incorrectly considers all negative numbers as even.
x % 2 != 0 or
(x & 1) == 1 instead.