If you check after dereferencing it, yes it can. But in this case why would you not check before dereferencing? It's the only UB-free choice.
If you check after dereferencing it, yes it can. But in this case why would you not check before dereferencing? It's the only UB-free choice.
To be more precise GCC says "eliminate useless checks for null pointers" and what I am saying that you can never be sure what in your code ended up being "useless check" vs "useful check" according to the GCC dataflow analysis.
Linux kernel is a famous example for disabling this code transformation because it is considered harmful. And there's nothing harmful with the nullptr check from your example.
Right. It's UB. And that's why the optimization in question is about removing that check. The only reason the optimization is valid for a C compiler to do, is that it can assume dereferencing a null pointer lands you in UB land.
I'm sorry, either you are terrible at trying to explain things, or you have thoroughly misunderstood what all this is about. GCC cannot, under any circumstances or with any flags, remove an "if (ptr == NULL)" that happens before dereferencing the pointer.
What this flag is about, and what the kernel bug you mentioned (at least I think you're referring to this one) is about, was a bug that went "int foo = ptr->some_field; […] if (ptr == NULL) { return -EINVAL; }". And GCC removed the post-deref null pointer check, thus making the bug exploitable.
From the help text:
> if a pointer is checked after it has already been dereferenced, it cannot be null.
after. Only applies after. A check before dereferencing can never be removed by the compiler.
Obviously.
if (ptr == NULL)
{
...
}
...
int foo = ptr->some_field;But this brings us back to the article: Why does the author say that there's no way to check for NULL in the free function? Maybe they are hinting at something completely unrelated to what we're saying here?
If that's where we failed to communicate, then that makes sense. Thanks, stranger!
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.I don't think so. If it could, then this code would reliably crash:
char *mystr = strdup (oldstr);
if (mystr)
*mystr = 0; // Truncate string
That never crashes.