Undefined Behavior
Program behavior on which a language specification places no requirements at all. Once a program triggers it, anything can happen.
Also known as: UB, Undefined Behaviour, Nasal Demons, Unspecified Behavior, Implementation-Defined Behavior
Category: Software Development
Tags: programming, software-engineering, security, computer-science, vulnerabilities, code-quality
Explanation
Some language specifications, with C and C++ as the best-known cases, declare certain operations to be undefined. If your program does one of them, the standard says nothing about what happens next. It can crash, give a wrong result, or seem to work. The compiler is allowed to assume that it never happens.
**Classic examples in C and C++**:
- Reading or writing outside the bounds of an array
- Dereferencing a null pointer
- Using memory after it was freed
- Signed integer overflow
- Reading a variable that was never initialized
- Data races between threads
**Three categories that are easy to confuse**:
- **Implementation-defined**: Each compiler chooses and documents its choice. Example: the size of an `int`
- **Unspecified**: The compiler picks one of several valid options and doesn't have to say which. Example: the order in which function arguments are evaluated
- **Undefined**: No requirements at all
**Why it exists**:
- **Performance**: The compiler doesn't have to add checks, and can optimize on the assumption that the code is correct
- **Portability**: The same language can run on very different hardware without forcing one behavior that is slow on some machines
**Why it's dangerous**:
- **Optimizers reason backwards from it**: If dereferencing a pointer would be undefined when it's null, the compiler concludes that the pointer isn't null, and removes your later null check
- **It works until it doesn't**: The code passes every test for years, then breaks with a new compiler version or another optimization level. The program relied on what one compiler happened to do, not on what the language promised (see Hyrum's Law)
- **It's the root of most memory safety vulnerabilities**
**What to do**:
- Use sanitizers and static analysis in testing, such as AddressSanitizer and UndefinedBehaviorSanitizer
- Prefer languages that are memory safe. Safe Rust, for example, rules out undefined behavior
- Turn on compiler options that define the behavior, when they exist
**The lesson for designers**:
Every gap in a specification gets filled by implementations, and each one fills it differently. For a protocol or an API, that means interoperability bugs and security holes. Define what happens for every input, including the invalid ones.
Related Concepts
← Back to all concepts