|
|
|
|
|
by knorker
260 days ago
|
|
That godbolt illustrates what I've been saying, yes. The godbolt example is exactly what I meant when I said "If you check after dereferencing it, yes it can [remove the check]". In fact we can make the example even shorter: https://godbolt.org/z/8fv4GKMse And here's one with a "time travelling" removal of a branch because of UB: https://godbolt.org/z/PjYzqKxs4 Remove the memcpy, and it adds the comparison back in: https://godbolt.org/z/r1GTvGnnf This one is fun too: Explicitly crash on null pointer: https://godbolt.org/z/sGzhK7zM4 As this comment (https://news.ycombinator.com/item?id=45442103) says, it looks like we may have just been talking past each other. When I said "If you check for null pointer before you dereference, then no the compiler cannot elide the check." I meant like: if (ptr != NULL) { *ptr = 123; }
Which is obviously safe and the compiler cannot remove the check. Not: if (ptr != NULL) { [… something else …] } *ptr = 123;
The latter is UB, and UB is allowed to time travel. |
|