Blog post shows GCC 16.2 can execute unrelated code after undefined behavior
A developer's personal blog post demonstrates that GCC 16.2, when compiling C code that triggers undefined behavior (via std::unreachable() or a null pointer dereference), can generate assembly that executes a completely separate, never-called function. In one example, writing to a 'device_command' variable followed by undefined behavior causes the compiler to also emit code for an unrelated 'explode()' function that was never invoked in the source.
GoKawiil's interpretation of the reporting above, not reported fact.
The post argues against a common misconception that undefined behavior in C merely risks a predictable crash or segfault, showing instead that compilers can generate arbitrary, unexpected instructions once UB occurs. This could matter for safety-critical or embedded systems code, where silent execution of unintended logic—rather than a clean crash—might have serious consequences, according to the author's examples using memory-mapped device control code.
- GCC 16.2 at -O1 was shown to execute an unrelated function after undefined behavior was triggered via std::unreachable()
- A null pointer dereference example showed the compiler writing zero to address zero rather than the intended value before crashing
- The post challenges the assumption that undefined behavior in C is limited to predictable outcomes like segfaults
The C Programming Language (K&R) book — If undefined behavior keeps surprising you, it helps to go back to the source and deeply understand C semantics rather than assume safety where none is guaranteed. This classic text lays the groundwork for understanding why the language leaves certain behaviors undefined in the first place. A solid reference for any systems programmer wrestling with compiler optimizations and UB.
See The C Programming Language (K&R) book on Amazon → Affiliate link — we may earn a commission on purchases, at no extra cost to you. Product picked by AI based on this article; it is not a tested recommendation.Source: blog.habets.se, 2026-10-25
Published there as: “Attention people who think UB is fine”
Read the original report → The summary and analysis above are GoKawiil's own, written from reporting by the source above. Facts and quotes belong to the original publisher.