Discover and fix undefined behavior in your code.
- Xcode 9.0+
The Undefined Behavior Sanitizer, or UBSan, is an LLVM tool for C languages that detects undefined behavior at runtime. Undefined behavior describes the result of any operation with unspecified semantics, such as dividing by zero, loading memory from a misaligned pointer, or dereferencing a null pointer.
When a program has undefined behavior, it may cause a crash, incorrect program output, or no problem at all. It can also make a program function differently depending on the version of a compiler that was used. For these reasons, undefined behavior should be avoided.
How UBSan Works
The Undefined Behavior Sanitizer works by using Clang to emit checks into your code during compilation. The nature of the inserted code depends on the kind of undefined behavior being checked for. For example, the Integer Overflow check adds code around arithmetic operators, including addition, subtraction, multiplication, and division.
The performance impact of the Undefined Behavior Sanitizer is minimal, with with an average 20% CPU overhead in the Debug configuration.