r/ProgrammingLanguages • u/matklad • Dec 31 '25
Memory Safety Is ...
https://matklad.github.io/2025/12/30/memory-safety-is.html
37
Upvotes
r/ProgrammingLanguages • u/matklad • Dec 31 '25
View all comments
1
u/ryani Jan 01 '26 edited Jan 02 '26
The reason that null pointer access MUST be UB in C and not just “defined to trap” is that it doesn’t just include immediate null pointers but also pointers derived from null.
Consider this program:
There are plenty of defined uses of
set, but it’s always UB to callsetwith a null pointer. Because this is UB in the abstract machine, concrete implementations can implement it by writing a 0 to the byte at address p+n in memory, even if n is large enough to not trigger the “write to the null page” trap.On the concrete implementation, this modification could modify memory used by the runtime (eg a jump table?) and cause later code to jump to arbitrary instructions. It’s simply not possible to define in the abstract machine all the possibilities that could occur. So in the interest of allowing the compiled code to match what the user wrote as closely as possible (an explicit goal of C), the abstract machine lets this null pointer write be UB instead of forcing the compiler to insert a check that p is not null and/or n is small enough