How might we eliminate undefined behaviour?
Posted Jul 20, 2009 1:41 UTC (Mon) by
mikov (subscriber, #33179)
In reply to:
How might we eliminate undefined behaviour? by xoddam
Parent article:
Linux 2.6.30 exploit posted
A poster in The Register (Steve 105) gave a good example of why a warning in this case, while it does seem desirable at first sight, is probably not a good idea in general:
Surely the reason for the optimisation is (among other things) code like this:
inline char foo(char *p) { if (p == 0) return 0; else return *p; }
char bar(char *p) { *p = 2; return foo(p); }
int main() { char c = 0; return bar(&c); }
If foo gets inlined into bar, the compiler can spot that the null pointer check in the inlined code is unnecessary and remove it. This is a most excellent optimisation (granted, in this example foo and bar do so little work that other optimisations may render it unnecessary).
I think GCC would do good if it reported this a warning iff using the pointer and checking the pointer are in the same original function - that is the optimization didn't appear as a result of other optimizations like global inlining, etc.
That would have caught our case. I am not sure how difficult it would to implement this distinction though - I suspect quite.
(
Log in to post comments)