skip to content

Why can a C++ program with undefined behaviour work in a -O0 debug build yet misbehave in a -O2 release build?

level: juniorimportance: should knowfreq 60%

answer

  1. the bug was always there
  2. -O0 translates almost literally
  3. the optimizer assumes no UB
  4. layout and registers change too

basics

~20 s

Undefined behaviour imposes no requirements, so the optimizer may assume it never happens. At -O0 the compiler translates each statement almost literally and the bug often stays hidden; at -O2 transformations built on that assumption expose it.

solid answer

~40 s

The standard places **no requirements** on an execution that contains undefined behaviour, and an optimizing compiler is entitled to assume a valid program never reaches it. At `-O0` the compiler emits nearly statement-by-statement code, keeps variables in memory and inlines little, so the bug often does what the source seems to say. At `-O2` passes such as inlining, value-range analysis and loop optimization reason from the assumption: an overflow check that can only fire after signed overflow is folded to `false`, and a loop with no I/O, `volatile` or atomic access is assumed to terminate. Stack layout and register use change too, so an overrun or uninitialized read lands on different data. The debug build was never correct, only lucky; the fix is to remove the undefined behaviour, not to lower the optimization level.

code

cpp · 14 lines
cpp
#include <cstdio>

// Returns 1 once v reaches x. For an even x, v (always odd) never gets there.
int reaches(unsigned x) {
    unsigned v = 1;
    while (v != x)       // no I/O, volatile or atomic access in the loop:
        v *= 3;          // the compiler may assume it terminates
    return 1;
}

int main() {
    std::printf("%d\n", reaches(9));   // terminates: 1 -> 3 -> 9
    // reaches(2) can never terminate, so its behaviour is not defined.
}

go deeper

for a junior

Remember the rule of thumb: if behaviour changes with the optimization level, suspect undefined behaviour in your code before suspecting the compiler.

for a middle

Explain what -O0 and -O2 do differently and how the assumption that undefined behaviour never happens lets the optimizer drop checks and loops.

for a senior

Describe how you reproduce a release-only failure with an optimized debug-info build and sanitizers, and why lowering the optimization level only hides the defect.

for a principal

Argue for CI that tests optimized builds with sanitizers, so release-only defects surface before shipping rather than in production.

## The bug is in the source, not in the optimizer Undefined behaviour is behaviour for which the C++ standard imposes no requirements. A conforming compiler only has to reproduce the observable behaviour of executions that are well-defined; for the rest it may do anything. Optimizers turn that into a working assumption: **a valid program never executes undefined behaviour**, so any fact implied by "this operation was valid" may be used to simplify the code. That is why "it works in debug" is not evidence. Both builds are correct translations of the same program; the program simply has no defined meaning on the input that triggers the bug. ## What changes between -O0 and -O2 At `-O0` a compiler such as Clang or GCC aims at fast compilation and easy debugging: - each statement becomes its own small block of instructions, in source order; - local variables live in stack slots and are reloaded on every use; - almost nothing is inlined, so each function is analysed alone; - no pass reasons about value ranges or loop trip counts. At `-O2` dozens of passes run, and several of them draw conclusions from the rules of the language: | Optimization | Assumption it relies on | What a bug turns into | |---|---|---| | Folding `x + 1 < x` for `int x` to `false` | signed arithmetic never overflows | the overflow check disappears | | Deleting `if (p == nullptr)` after `p->len` | a dereferenced pointer is not null | the null guard disappears | | Removing a side-effect-free loop | every thread eventually makes progress | a loop that should hang returns | | Keeping a variable in a register | no other thread races on it | a polling loop never sees the update | | Packing locals differently on the stack | nothing reads past an object | an overrun corrupts a different variable | The details belong to the other lessons in this branch; what matters here is the pattern. None of these is a compiler bug. ## A loop that hangs in debug and returns in release The function in the code example multiplies `v` by 3 until it equals `x`. For an even `x` the loop can never finish, because `v` stays odd. The loop performs no I/O, no `volatile` access and no atomic or synchronization operation, and the standard lets the implementation assume that every thread eventually terminates or does one of those things. Apple clang 21 therefore compiles `reaches` at `-O2` to two instructions that return 1, while the `-O0` build of `reaches(2)` spins until it is killed. The source says "loop forever", the optimized program says "return 1", and both are permitted because the non-terminating case is outside what the standard defines. ## Why the symptoms move around Optimization changes more than the arithmetic: 1. **Memory layout.** An off-by-one write that hit unused padding at `-O0` may hit a live variable once locals are packed or kept in registers. 2. **Uninitialized values.** At `-O0` an uninitialized local often reads a stack slot that happens to hold a plausible value; at `-O2` it may be whatever a register held last. 3. **Inlining.** Once a callee is inlined, facts from the caller, such as "this pointer was already dereferenced", flow into it and trigger simplifications that the separately compiled function never saw. 4. **Timing.** Data races that never overlapped in a slow debug build start to overlap. ## What to do about it - Treat a debug-versus-release difference as a symptom of undefined behaviour, or of reliance on unspecified behaviour, until proven otherwise. - Reproduce it in an optimized build with debug information, then use the sanitizers and warnings covered with the compiler toolchain to find the faulting line. - Do **not** "fix" it by dropping to `-O1`, sprinkling `volatile`, or adding a `printf` that makes the problem vanish: the undefined behaviour is still there and will return with the next compiler upgrade. - Remember that `-O0` gives no protection either: undefined behaviour can misbehave at any level, and a new compiler release can change the `-O0` output too. - Keep the optimized configuration in your test matrix from day one; a team that only runs tests in debug builds discovers these bugs from customers.

  • Is a C++ build at -O0 guaranteed to show the 'literal' behaviour of code with undefined behaviour?
    No. `-O0` is a compiler's choice to optimize little, not a mode the standard defines. Compilers fold some expressions even at `-O0`, and a new release can change the generated code at any level. Undefined behaviour is undefined at every optimization level; `-O0` merely tends to hide it.
  • Why doesn't the compiler warn every time it relies on undefined behaviour to optimize?
    The assumptions are used everywhere, for example in nearly every loop with an `int` counter, so a warning on each use would flood every build. The compiler usually cannot tell a check that is dead because of real constraints from one that is dead only because of a bug. Some cases are warned about, such as tautological pointer comparisons, and sanitizers catch the rest at run time.

A proofreader who assumes the author never writes impossible sentences: shown a sentence that contradicts itself, the proofreader quietly rewrites it into something tidy, and the meaning the author 'obviously intended' is gone.

  • The optimizer has a bug if a program behaves differently at -O2 than at -O0.
  • Building release with -O0 or -O1 is a valid fix for an optimization-only crash.
  • Code that passes all tests in the debug build has no undefined behaviour.
  • Adding a printf that makes the bug disappear proves it was a timing issue.