3 ms·
When you're saying that the compiler should be allowed to optimise under the assumption that OOB array accesses don't clobber other variables, this means that O
by caf 11y ago
When you're saying that the compiler should be allowed to optimise under the assumption that OOB array accesses don't clobber other variables, this means that OOB array accesses can make very weird things happen indeed. For example:
if (a > 1)
b++;
array[b] = 0;
if (a > 1)
foo(a);
We might see foo() called with argument 0 when that's apparently impossible - the OOB write has apparently "reached forward" (it can also "reach backward").
This is why it ends up just being "undefined behaviour" - to do better either you have to somehow exhaustively document all the kinds of weird things that can happen ("it writes to the memory" isn't enough, because of the way that can interact with the optimiser) or you have to unreasonably constrain the optimiser.