skip to content

With GCC, what does -fsanitize=undefined check, and why can a CI job pass despite UBSan reports unless you add -fno-sanitize-recover?

level: middleimportance: should knowfreq 52%

answer

  1. checks inserted around operations
  2. overflow, shifts, null, bounds, vptr
  3. report and keep running
  4. exit code may still be zero
  5. two float checks left out

basics

~20 s

-fsanitize=undefined inserts run-time checks for undefined operations such as signed overflow, bad shifts, division by zero and null or misaligned access. GCC enables recovery by default, so the program continues and may exit 0; -fno-sanitize-recover=undefined makes errors fatal.

solid answer

~40 s

UndefinedBehaviorSanitizer (UBSan) adds a check before each operation whose result could be undefined: `shift`, `integer-divide-by-zero`, `signed-integer-overflow`, `null`, `alignment`, `bounds`, `vptr`, `return`, `unreachable` and more. GCC's manual says recovery is turned on by default for UBSan, so after a report "the exit code of the program may indicate success even when errors have been reported", and the run-time also defaults to `halt_on_error=0`. In CI that means a green job with errors in its log. `-fno-sanitize-recover=undefined` (or `=all`) reports only the first error and exits non-zero. Two checks are not part of `-fsanitize=undefined` in GCC: `float-divide-by-zero` and `float-cast-overflow`, which must be named. The checks are cheap enough to combine with AddressSanitizer in one build.

code

cpp · 13 lines
cpp
#include <climits>
#include <cstdio>

int add_bonus(int score, int bonus) {
    return score + bonus;               // signed overflow is undefined behaviour
}

int main() {
    int total = add_bonus(INT_MAX, 1);
    std::printf("total computed\n");
    (void)total;
    return 0;
}

go deeper

for a junior

Know that -fsanitize=undefined reports undefined operations such as signed overflow at run time, with the file and line.

for a middle

Explain recovery mode, why the default can leave the exit code at zero, and which float checks are not included.

for a senior

Configure CI so UBSan errors fail the job, and choose between reporting, recovery for triage, and trap mode.

for a principal

Set the rule that every sanitizer job fails on its first report, and decide when batch triage in recovery mode is allowed.

## What UBSan checks **UndefinedBehaviorSanitizer** instruments "various computations ... to detect undefined behavior at runtime". Instead of shadow memory, it inserts a small check before each risky operation. `-fsanitize=undefined` is an umbrella; GCC 16's manual lists suboptions including: - `-fsanitize=shift` (with `shift-base` and `shift-exponent`) - `-fsanitize=integer-divide-by-zero` and `-fsanitize=signed-integer-overflow` - `-fsanitize=null` and `-fsanitize=alignment` - `-fsanitize=bounds` for array indexing where the bound is known - `-fsanitize=vptr`, which checks that a C++ member call or downcast uses an object of the right dynamic type - `-fsanitize=return` and `-fsanitize=unreachable` - `-fsanitize=pointer-overflow`, `-fsanitize=builtin`, `-fsanitize=nonnull-attribute` and `-fsanitize=returns-nonnull-attribute` What counts as undefined is a language question, covered with undefined behaviour in C++; this flag is about catching it at run time. ## Two checks it leaves out | Check | In `-fsanitize=undefined`? | Why, per the manual | |---|---|---| | `float-divide-by-zero` | no | dividing by zero "can be a legitimate way of obtaining infinities and NaNs" | | `float-cast-overflow` | no | must be requested separately; does not work well with `FE_INVALID` exceptions | A build that wants them writes `-fsanitize=undefined,float-cast-overflow`. ## Recovery: the CI trap The GCC manual's entry for `-fsanitize-recover=` is explicit: 1. With recovery on, a sanitizer "attempt[s] to continue running the program as if no error happened", so several errors can be reported in one run, and "the exit code of the program may indicate success even when errors have been reported". 2. Recovery "is turned on by default" for `-fsanitize=undefined` and its suboptions, except `unreachable` and `return`. 3. The run-time library defaults to `halt_on_error=0` for UBSan and ThreadSanitizer, and to `halt_on_error=1` for AddressSanitizer. 4. `-fno-sanitize-recover=` reverses it: "only the first detected error is reported and program then exits with a non-zero exit code". Running the example below with Apple clang 21, which behaves the same way, showed the effect: the plain UBSan build printed the overflow report, kept going and exited 0; the `-fno-sanitize-recover=undefined` build printed the same report and exited with status 134. So a CI job that only checks the exit status will pass with UBSan errors in its log unless it uses `-fno-sanitize-recover`, or sets `UBSAN_OPTIONS=halt_on_error=1` for a recoverable build, or scans the log for `runtime error:`. ## Trapping instead of reporting `-fsanitize-trap=` makes a failed check call `__builtin_trap` instead of a `libubsan` routine. The trap is fatal even if recovery is enabled, there is no report text, and `libubsan` is not linked, so the manual notes it is usable even in freestanding environments. `-fsanitize=vptr` always needs `libubsan` and cannot trap. ## Cost and combinations - Clang's documentation describes UBSan's checks as having "small runtime cost" and no impact on address space layout or ABI. - It combines with AddressSanitizer: `-fsanitize=address,undefined` is a common single test build. - GCC's manual notes that `-ftrapv` traps on signed overflow, while `-fsanitize=undefined` gives a diagnostic message. ## Practical rules - Always pair UBSan in CI with **`-fno-sanitize-recover=undefined`** or `=all`. - Add **`float-cast-overflow`** when code converts floating values to integers from untrusted input. - Build with **`-g`** so reports name the file and line.

  • When is recovery mode actually useful?
    On a large test suite where you want every distinct UBSan error from one long run instead of fixing them one per run. Collect the log, deduplicate by source location, and fix them in a batch; then switch CI back to `-fno-sanitize-recover` so new errors fail the job.
  • Why would you add -fsanitize=float-cast-overflow explicitly?
    Converting a floating value outside the target integer's range is undefined, and GCC's `-fsanitize=undefined` does not include this check. Code that turns parsed or computed doubles into indices or sizes should enable it, so an out-of-range value is reported instead of producing an arbitrary integer.

  • A UBSan report always makes the test process exit non-zero
  • -fsanitize=undefined also checks floating-point division by zero
  • UBSan cannot be combined with AddressSanitizer
  • -fsanitize-recover makes the first error fatal
  • UBSan needs shadow memory like ASan and doubles memory use