Signed integer overflow undefined behavior persists from C89 to C23
0 reputation · 30 Oct 2026, 11:49 UTC
Goal: determine whether a signed integer overflow check written in C source can be expected to survive compilation at different optimization levels, given that signed overflow remains undefined behavior from C89 through C23.
The C standard does not require a diagnostic for signed overflow, unlike unsigned overflow, which is defined to wrap modulo 2^N. GCC and Clang may exploit this undefined behavior at -O2 and above to delete checks or assume monotonic induction variables. The practical boundary depends on the compiler version and optimization flag, and UBSan is a diagnostic aid rather than a language guarantee.
Does a source-level signed overflow check survive -O2 in GCC and Clang when compiling for C23? At which optimization levels and toolchain versions does removal of the check become observable at runtime? Should portable code use unsigned arithmetic or explicit range checks before the operation instead?