Article

Invalid Boolean

Detects when a program accesses a Boolean variable and its value isn’t true or false.

Overview

This check detects accesses a Boolean variable when its value isn’t true or false. This problem can occur when an integer or pointer is used without an appropriate cast. The use of out-of-range Boolean values has undefined behavior, which can be difficult to debug.

Invalid Boolean Variable Access in C

The intent of the following code is to call the success function when result is nonzero. However, because a Boolean check is used, the compiler may, as an optimization, only emit instructions that check the least-significant bit of predicate, which is 0, causing a logic error.

int result = 2;
bool *predicate = (bool *)&result;
if (*predicate) { // Error: variable is not a valid Boolean
    success();
}

Solution

Use integer comparison instead of a Boolean check.

int result = 2;
if (result != 0) { // Correct
  success();
}

See Also

Undefined Behavior Checks

Misaligned Pointer

Detects when code accesses a misaligned pointer or creates a misaligned reference.

Out-of-Bounds Array Access

Detects out-of-bounds access of arrays.

Invalid Enumeration Value

Detects when an enumeration variable has an invalid value.

Reaching of Unreachable Point

Detects when a program reaches an unreachable point.

Dynamic Type Violation

Detects when an object has the wrong dynamic type.

Invalid Float Cast

Detects out-of-range casts to, from, or between floating-point types.

Division by Zero

Detects division where the divisor is zero.

Nonnull Argument Violation

Detects when a null value is incorrectly passed as an argument.

Nonnull Return Value Violation

Detects when a function incorrectly returns a null value.

Nonnull Variable Assignment Violation

Detects when a null value is incorrectly assigned to a variable.

Null Reference Creation and Null Pointer Dereference

Detects the creation of null references and null pointer dereferences.

Invalid Object Size

Detects invalid pointer casts due to differences in the sizes of types.

Invalid Shift

Detects invalid and overflowing shifts.

Integer Overflow

Detects overflow in arithmetic.

Invalid Variable-Length Array

Detects array bounds that aren’t positive.