Skip to content
systemcoding.dev
Debugging

Stop on the first UBSan error with -fno-sanitize-recover

Make UBSan in Clang 18 stop at the first undefined behaviour with -fno-sanitize-recover=undefined, instead of printing and carrying on.

1 min read
Summarize in AI

By default UBSan prints an error and keeps running, so a long test run buries the first report under later ones and exits with status 0. Compiling with -fno-sanitize-recover=undefined makes each check fatal.

c
#include <limits.h>

int main(void) {
    volatile int big = INT_MAX;
    return big + 1;
}
Text
clang -g -fsanitize=undefined -fno-sanitize-recover=undefined demo.c -o demo
./demo; echo $?
Text
demo.c:5:16: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'
1

Without the second flag the same program prints the message and then exits with the wrapped value, and nothing tells a test harness that anything went wrong. The status 1 is what a CI job needs. You can name one check, -fno-sanitize-recover=signed-integer-overflow, and leave the rest recoverable, and all means every enabled check; GCC 14 accepts the same spelling. The volatile stops the compiler folding the sum at build time. Fatal checks suit tests; in a long-running service you may prefer to log and continue. See the Clang UBSan documentation for Clang 18.

Also inSecurity
Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.