GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
gcc/generic

Do not fold branchless min/max when overflow is sanitized

Prevent GCC from simplifying expressions that hide signed integer overflow warnings.

GCC’s generic matching logic incorrectly folded branchless min/max expressions when signed integer overflow sanitization was enabled. This simplification obscured the overflow, preventing the sanitizer from reporting it. The fix ensures that sanitized types are excluded from this folding, preserving the sanitizer’s ability to detect overflows.

In Details

The gimple_match_no_overflow_p function in generic-match-head.cc used TYPE_OVERFLOW_UNDEFINED as a proof of non-overflow for arithmetic operations. However, with -fsanitize=signed-integer-overflow, the front-end folds expressions like y + (x - y < 0 ? x - y : 0) into MIN_EXPR before pass_ubsan instruments the code. This commit modifies gimple_match_no_overflow_p to return false for sanitized types, preventing premature folding and ensuring overflow is detected.

For Context
GIMPLE
GCC's internal representation for code after the front-end has parsed it. It's a three-address, low-level form that simplifies optimization.
signed integer overflow
Occurs when an arithmetic operation on signed integers produces a result that is outside the range representable by the data type. In C/C++, signed overflow leads to undefined behavior.
UBSan (Undefined Behavior Sanitizer)
A runtime instrumentation tool in GCC that detects various types of undefined behavior, including signed integer overflow, which can lead to incorrect program behavior or security vulnerabilities.
folding
An optimization technique where constant expressions are evaluated at compile time rather than runtime.
Filed Under: gccubsanoptimizationsecurity