3 ms·
I found the old discussion here on HackerNews although I haven't seen it before. I only found it now because I wanted to double-check my hypothesis and see if m
by menaerus 1y ago
I found the old discussion here on HackerNews although I haven't seen it before. I only found it now because I wanted to double-check my hypothesis and see if my understanding is really off. Seems like not: https://news.ycombinator.com/item?id=42387013 https://news.ycombinator.com/item?id=42387013
https://godbolt.org/z/aPcr1bfPe https://godbolt.org/z/aPcr1bfPe
- knorker 1y agoThat 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 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 https://godbolt.org/z/PjYzqKxs4 Remove the memcpy, and it adds the comparison back in: https://godbolt.org/z/r1GTvGnnf https://godbolt.org/z/r1GTvGnnf This one is fun too: Explicitly crash on null pointer: https://godbolt.org/z/sGzhK7zM4 https://godbolt.org/z/sGzhK7zM4 As this comment (https://news.ycombinator.com/item?id=45442103 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.