All nontrivial C and C++ has undefined behavior
Why all nontrivial C and C++ contains undefined behavior, with ordinary examples, and the case for pointing an LLM at your code to find it.
Casting a const uint8_t* to a const int* is undefined behavior in C, and the dereference that comes after it isn’t even the issue. Creating the misaligned int* is already enough. A compiler is free to assign meaning to the low bits of an int*, for garbage collection or security tagging, so a pointer that doesn’t honor that invariant can’t exist in the model the compiler reasons about. Dereference it and you might get a clean read on x86, a kernel software-emulation on some Alpha loads, or a SIGBUS on other Alpha loads and on SPARC.
That is one of about a dozen examples in a recent post arguing that all nontrivial C and C++ contains undefined behavior, written by someone who has used both almost daily for roughly 30 years. The claim is stronger than the usual memory-safety complaint. Everyone already knows double-free, use-after-free, out-of-bounds access and reading uninitialized memory are UB. The post’s point is how much else is, and how little of it looks dangerous at the call site.
The most useful correction in it is about optimizations. A common belief is that UB only bites when you turn on -O2, as if the compiler is being deliberately hostile and backs off at -O0. UB doesn’t mean the compiler gets to exploit your sloppiness. It means the compiler is allowed to assume your code is valid, and the cases you wanted handled don’t need to exist in the code generator at all, because the standard says they can’t happen. Turning off optimizations doesn’t hand back a guarantee the language never made.
The examples matter because each is ordinary code:
isxdigit(ch)wherechis achar.isxdigittakes anint, because it also has to acceptEOF, which is anintnot representable asunsigned char. Ifcharis signed on your platform, which is implementation-defined, and the value is above 127, you pass a negativeint. A conformingisxdigitis allowed to use that value to index a table. The author’s point is that the resulting read could land on I/O-mapped memory, and since user-space network drivers exist, user space is not a guaranteed shield.- Converting a
floattoint. If the integral part isn’t representable in the target type, that’s UB; a non-finite float is UB by omission; and adding one to the result is separately UB through signed overflow. The post shows how much guarding it takes to turn seconds into integer milliseconds correctly, and notes how much shipping code just multiplies by 1000 and casts. memset(&ptr, 0, sizeof(ptr))is not guaranteed to produce a null pointer. You cannot zero-fill a struct and assume its member pointers are NULL. Historic machines used non-zero null pointers, and the standard only promises that NULL compares equal to the constant zero.- Variadic calls.
execl("/bin/sh", "sh", "-c", "date", NULL)is UB because NULL may be passed as an integer; it needs(char*)NULL.printf("%ld", some_uint64_t)is UB; use thePRIu64macro. - Divide by zero is UB, and the denominator is often attacker-controlled input, which makes it a security bug rather than just a crash.
C23 uses the word “undefined” 283 times, and that count leaves out everything undefined by omission. There is also the non-UB trap of integer promotion, where unsigned char a = 0xff, b = 1; makes (a + b) == 0 false, because both operands promote to int and the sum is 0x100, not a wrapped 0. Nobody applies those rules correctly at reading speed.
The part engineers running these codebases should take seriously is the suggested remedy. The author reports that pointing a current LLM at arbitrary C finds UB and is right almost every time. As a test he aimed one at OpenBSD’s find, got a list, and sent the project a patch for an out-of-bounds write plus a separate non-UB logic bug. He did not bother submitting the rest, citing the project’s past reception of bug reports and his sense that most of it is fine in practice.
His own workflow on code he owns is to have the LLM find, explain and fix UB, then stare at the diff until he can independently confirm both the defect and the fix. He is blunt that writing C or C++ in 2026 without an LLM checking for UB is irresponsible, on the grounds that if OpenBSD couldn’t keep it out over 30 years, the rest of us won’t either.
The limit he names is the one that matters operationally. Confirming the findings still requires an expert who understands the standard, and those people are usually busy with something else. It is janitor work, but too subtle to hand to the juniors who normally get janitor work.
Keep Reading
WindowsMicrosoft's November update broke SLMGR activation, fixed Friday
Microsoft shipped out-of-band updates on Friday to fix Windows activation failures its November 2026 security updates caused for SLMGR users.
supply-chain-attackOn 16 November, Optimizely served malicious JavaScript to 178 sites
On 16 November 2026, attackers injected malware into 178 brands' sites through Optimizely Web, swapping crypto addresses, skimming cards and hitting iOS.
openjdkSixty-four A's and an equals sign break OpenJDK's Base64
Java4Shell (CVE-2026-114938) is a heap overflow in OpenJDK's Base64 decoder that Calif turned into pre-auth RCE across widely deployed Java apps.
Latest on the Wire
Full wire →- AI Coding Agents Boost Output But Code Review Bottlenecks IncreaseHacker News
- AI Overviews Cut Organic Traffic by 40%Hacker News
- Apache Patches Critical Roller RCE VulnerabilityBleepingComputer
- Complete Web Browser Built from Scratch in PythonHacker News
New signal daily · RSS
Stay in the loop
New writing delivered when it's ready. No schedule, no spam.