4 ms·
You can also make it throw an error at runtime: $ gcc -fsanitize=undefined -o test test.c $ ./test test.c:3:10: runtime error: load of address 0x7ffea07f
by LegionMammal978 2y ago
You can also make it throw an error at runtime:
$ gcc -fsanitize=undefined -o test test.c
$ ./test
test.c:3:10: runtime error: load of address 0x7ffea07fded0 with insufficient space for an object of type 'int'
0x7ffea07fded0: note: pointer points here
b2 55 00 00 d8 df 7f a0 01 00 00 00 c8 df 7f a0 fe 7f 00 00 00 00 00 00 00 00 00 00 62 0f 73 ea
^
> But this is exactly the problem. UB can be anything, including nothing.
There's nothing stopping compilers from implementing the semantics you want (when not crossing an ABI boundary), and indeed, they've been adding more gradual hardening options that can be used in production. It's just that there's little demand for universal bounds-checking on arrays, and some users even want more flexible accesses, e.g., for operations like container_of.
On the other hand, people can and have made experimental forks of the Rust compiler to turn all panics into immediate LLVM-level UB, but the mere existence of such an option doesn't mean that Rust's bounds-checking is now worthless, as you seem to be implying for C.