Attention people who think UB is fine
Web page · https://blog.habets.se/2026/10/Attention-people-who-think-UB-is-fine.html
First seen by Oriel on .
AI summary
The post demonstrates that undefined behavior in C++ can produce radically different machine code, challenging the claim that it has predictable effects such as merely causing a segfault.
- With GCC 16.2 at -O1, `std::unreachable()` after a volatile write leaves `serve_hot_chocolate()` without a return, and the author says execution can continue into `explode()`.
- For a null-pointer write, GCC emits a zero store to address zero followed by `ud2`; the intended value 99 does not appear in the assembly.
- GCC and clang emit a write to address 40 for a null pointer incremented by 10, despite the expression also invoking undefined behavior.
- The post argues that compilers are not required to detect undefined behavior and illustrates three outcomes: continued execution, a surprising write followed by a crash, and an apparently literal write.
#c++ #undefined behavior #compiler optimization #assembly
Generated by gpt-5.6-sol. Check the original for details.
Metric trends
Last 30 days · Up to 120 recorded samples per source and metric, with reposts kept separate · First and latest retained
Points
Score reported by the source.
- Hacker News: 11, latest observation ; +8 over observed period since
- Lobsters: 77, latest observation ; +76 over observed period since
Comments
Comment count reported by the source.
- Hacker News: 8, latest observation ; +7 over observed period since
- Lobsters: 75, latest observation ; +75 over observed period since
Aliases
| Source | External ID | URL |
|---|---|---|
| Hacker News | 50227814 | https://blog.habets.se/2026/10/Attention-people-who-think-UB-is-fine.html Read discussion |
| Lobsters | 5xzuff | https://blog.habets.se/2026/10/Attention-people-who-think-UB-is-fine.html Read discussion |