2 ms·
No, I meant what I said. The famous kernel “null pointer check removal” bug was in code of the form void foo(int *bar) { int baz = *bar; if (!bar
by saagarjha 6y ago
No, I meant what I said. The famous kernel “null pointer check removal” bug was in code of the form
void foo(int *bar) {
int baz = *bar;
if (!bar) {
return;
}
/* do stuff with baz */
}
which you can see provably invokes undefined behavior if bar is NULL. In the case you provided where the dereference comes after a function call the compiler cannot optimize out the check unless it can prove that control flow returns, which you claim it does not (it exits). noreturn and such are there to improve these cases and make them explicit to the compiler, but in the absence of information it must be conservative as there are many ways for a function call to never return, one of which is exit(3). Optimizing this case would be incorrect (i.e. a compiler bug) and this happens to be one of the reasons why function calls often serve as a barrier to optimizations in C.