Newer
Older

Undefined behaviour is defined, actually

Date: 2026-11-18 20:04
Tags: programming

The C and C++ languages have this concept of "undefined behaviour". It covers stuff like dereferencing a freed pointer or NULL, indexing an array out of bounds, integer overflow, writing to a read-only object, deleting a polymorphic object through a non-virtual base class pointer, and a bunch of other stuff that isn't allowed but the compiler can't stop you from trying to do. Broadly, if your program does one of these things, it messed up and it might crash or misbehave.

Programmers in these languages know this and try to avoid it so their programs work properly.

(Why can't the compiler prevent you from doing disallowed things? C and C++ are designed to be implemented without safety checks. If you want safety, you normally use a different language. Some of these undefined things are hard to check for without adding a lot of runtime overhead.)

The precise definition from C++23 is as follows:

undefined behavior:
behavior for which this document imposes no requirements

and a separate section clarifies further:

A conforming implementation executing a well-formed program shall produce the same observable behavior as one of the possible executions of the corresponding instance of the abstract machine with the same program and the same input. However, if any such execution contains an undefined operation, this document places no requirement on the implementation executing that program with that input (not even with regard to operations preceding the first undefined operation).

By the way, "undefined behaviour" and "ill-formed, no diagnostic required" mean exactly the same thing, and seem to be chosen according to the preference of whoever wrote each part of the standard.

So the picture is pretty clear: If your program would execute undefined behaviour, all bets are off, and they mean all bets. It can crash, it can corrupt data, it can work exactly as you intended, it can appear to work exactly as you intended unless it's Tuesday, it can reformat your hard disk, or as the internet memes go, it can make demons fly out of your nose.

Except wait a minute. Zoom. Enhance.

behavior for which this document imposes no requirements

Ah. It's only the language standard that doesn't impose requirements on the program's behaviour. Other things still might.

My computer runs a multi-user operating system, which guarantees no program will reformat my hard disk without asking me for a password. It can't invoke olfactory wraiths, because it's not plugged into my nose, nor to the depths of Hell. There's not even a standard USB protocol for that. And gcc will never compile Rust, no matter how much undefined behaviour you feed through it.

As you might have guessed, I'm posting this to clear up some misconceptions I see people have. Some people believe undefined behaviour means the computer can do absolutely anything. When reality is taken into account, this is patently untrue, and I always recommend taking reality into account.

To understand the actual limits of undefined behaviour, it helps to know the context in which it developed and how it evolved.


The original intention

When C was originally being standardized, C compilers were still fairly dumb, and the point of standardisation was to say what would always happen in every C compiler, even on different computers with different capabilities. At that time, computers didn't even all agree that a byte had 8 bits in it. So a lot of gaps were left in the standard to cover for differences between different computer designs.

For example, when the program runs on a computer that has a memory protection unit, dereferencing null raises some kind of fault. If the standard said that it must fault, then compilers for computers without memory protection would have to artificially check for null every time a pointer is dereferenced. This would add a few CPU cycles to every pointer dereference, which is undesirable. On the other hand, if they said that it must return 0, or something like that, then compilers for computers with memory protection would have to add a few cycles. By leaving it undefined, each compiler is allowed to just write out a dereference instruction, and whatever happens when you dereference a null pointer in that computer's assembly language will also happen when you do it in C.

Signed overflow is undefined for a similar reason. Some computers stored signed numbers using sign-magnitude representation, where 127+1 is -0 (negative 0). Others used two's complement or one's complement, where it's -128 or -127.

There was an implicit assumption that whatever happened would be reasonable. Nobody would buy a CPU that caught fire if you dereferenced null. People did buy CPUs that would catch it as a fault, and they also bought cheaper CPUs that wouldn't catch it. Nobody would make a CPU that played the Macarena from a hidden speaker if you dereferenced null. Or one that played Nethack if you calculated 127+1.

Exploitation for optimisation

Later on, in the 2000s, when compiler optimisation was getting attention, some compiler authors noticed that "literally anything" allowed for some good optimisation opportunities that compose well with inlining. If you write code like this contrived example:

int maybe_square(int* x) {
	if(x == NULL) {
		printf("hey dummy, your pointer is null\n");
		return 0;
	} else {
		return *x * *x; // the second * is multiplication
	}
}
int square_and_add(int* x) {
	return *x + maybe_square(x);
}

the compiler may notice that in square_and_add, if x was null, the first *x would be undefined behaviour and for the rest of the function it can assume it isn't null. After inlining and assuming non-null, it might compile the function like this:

int square_and_add(int *x) {
	int xx = *x;
	return xx + xx*xx;
}

This is what you want and expect. A lot of C++'s performance comes from the compiler inlining things many levels deep, and then culling all the unnecessary branches, resulting in assembly code that's specialised for the exact thing you're trying to do.

But that same rule means if you write code like this other contrived example:

int main(int argc, char **argv) {
	if (argc >= 0) {
		*(char*)NULL = 42;
	} else {
		printf("argc = %d\n", argc);
	}
	return 0;
}

the compiler may assume that the program never gets into the if branch, and then it may delete the if and its branch, leaving only printf(...). Then when you run the program it can print "argc = 1" even though that's only supposed to happen if it's negative.

At least, in principle it can do this. When I actually tried it, it wrote an illegal instruction in the if branch, probably because it matched the pattern of an intentional crash.

Constraints still apply

Early versions of gcc would invoke nethack (an early text-mode RPG) every time you used #pragma. This was technically allowed, because all use of #pragma is implementation-defined behaviour, which is the same as undefined except that it has to be documented. This was soon removed because it was very annoying and hurt compatibility.

So clearly there is a limit to how annoying compiler authors are willing to make their compilers. They probably wouldn't add hard-drive-formatting or rhinic-spectre-expelling code to your program without reason. They already learned from the #pragma incident that joke implementations of UB aren't good.

There are several mundane forms of undefined behaviour — such as putting a line break in the middle of a Unicode escape sequence. Clearly the intention is that compilers may parse it differently so you shouldn't write it like that. If your compiler chooses to emit an error, ignore the line break, or treat it as the wrong character, fine. If your compiler chooses to print an ASCII-art goatse, you should return it for a refund.

Undefined behaviour is only in source code

This is where I justify the provocative title. Although some CPUs feature some undefined behaviour, most things that are undefined behaviour in C or C++ are defined behaviour in CPUs.

A CPU treats a load or store instruction with address 0 the same as any other address — it looks up the address translation through the MMU, faults if the page isn't mapped, and otherwise accesses the corresponding physical address. This is defined. The CPU has no such thing as a null pointer because it has no such thing as null pointers. 0 is just another address.

Likewise, writing to a read-only page is defined — it always faults. Signed integer overflow is defined — indeed the CPU doesn't even know whether you think the integer is signed or unsigned. Indexing out of bounds isn't defined because indexing doesn't exist at all — the compiler converts it to address arithmetic, which is always defined. Some UB exists in some CPU architectures, but you don't really encounter it accidentally the way you do in C and C++. The nature of the fault is also defined: the CPU will switch to kernel mode, exchange the stack pointer with an MSR, push the program counter, and so on, and the operating system takes it from there. It's 100% predictable.

So this stuff is only undefined as far as the source code is concerned. Once you compile it and get code for your CPU, that CPU code has defined behaviour, even though you might not know which one. If the compiler removes one side of an if, the compiled program will consistently act that way. If the compiler emits a load from address zero, the compiled program will fault, unless it mapped address zero first, and it will do that consistently.

I once described undefined behaviour as "implementation-defined, no documentation required", to riff on another term the C++ standard uses. It's accurate. The program will do something, and that something is decided by the implementation. If you spent years poring over the compiler's code and the CPU's documentation, you could predict it in precise detail.

You should still avoid it

I'm not trying to tell you that undefined behaviour is predictable and you should incorporate it in your program. But I don't think you should avoid it because demons might fly out of your nose, either. If you did, you'd be programming by superstition, and possibly part of a cult. Nor do I think you should avoid it because "if your program contains UB then it has no semantics" — that's ivory-tower academic nonsense that doesn't actually mean anything. (Every program that compiles has semantics, and you can run it to see what they are.)

However, there are very rational, reality-based reasons to avoid UB. You should avoid it because it's unreliable — future updates to your compiler or operating system are likely to break it, and it likely doesn't work today on some compilers or operating systems. And when it breaks, it may be quite hard to troubleshoot.

You should be aware that things that are undefined in the language standard might not be undefined on every platform. For example, Windows guarantees that accesses to aligned 4-byte variables are always atomic. Although concurrent access to such a variable is explicitly undefined in the C++ standard, on Windows it is defined. Note this still doesn't provide memory ordering. There could be compilers for Windows that only apply the C++ rules and not the Windows rules; if so, those compilers are broken.

Occasionally, for various reasons, you might find it useful to rely on how a certain compiler actually implements certain UB. For example, if you know your compiler treats signed integer overflow in the two's complement way, even though it doesn't promise to, this might allow some algorithms to be written more efficiently. If you ship the compiled program to your customer, not the source code, then you may be shipping them a correct version of the algorithm even though the source code contains UB. If you're relying on this, you should be conscious that you are.