3 ms·
That would've already crashed at the p->some_field if p == NULL. > the compiler is free to assume that `p != NULL` is always true Only until the first point i
by userbinator 13y ago
That would've already crashed at the p->some_field if p == NULL.
> the compiler is free to assume that `p != NULL` is always true
Only until the first point in the "..." part where it cannot prove that p has not been modified.
- chowells 13y agoIf x is unused, the compiler is allowed to remove the assignment. If it does that optimization after it removes the "redundant" null pointer test, the optimizer has legally altered your program to remove the null pointer check you thought you had. http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_14.html http://blog.llvm.org/2011/05/what-every-c-programmer-should-... Please read that article, and the rest in the series. Undefined behavior is far more pervasive than you think.
- userbinator 13y ago> If x is unused, the compiler is allowed to remove the assignment This and the Apple SSL bug makes me think that optimising compilers should be far more explicit (i.e. emitting messages or even warnings) about what they're doing than they are now, because it seems far too much about the optimisation process is being hidden and not transparent enough. Not only unreachable code, messages like "result of computation x is never used", "if-condition assumed to be {false, true}", "while-loop condition always false - body removed", etc. would be extremely useful for detecting and fixing these problems.
- mansr 13y agoGCC can produce all of those warnings.
- theseoafs 13y agoWhy would you expect the NULL pointer check to do anything? The function, as given, is meaningless if `p` is NULL. The problem isn't that compilers are too aggressive in their optimization -- the problem is that people who are learning C don't usually learn what it means for the behavior of a program to be undefined.
- azakai 13y ago> That would've already crashed at the p->some_field if p == NULL. 1. Typically yes, and that is exactly why the compiler can remove the check after it, it's already undefined behavior so it can assume the optimistic case (and remove the check). 2. But it is not always true that it would crash. p->some_field might happen to be in a valid memory location to be accessed. This doesn't happen normally because low memory addresses (0-1024, say) tend to not be accessible by userspace programs. But I am not aware of a spec ensuring that. An OS could in theory let your program map memory at address 4, in which case p->some_field would succeed if p is NULL and the offset if 4.
- deleted 13y ago[deleted]
- bdonlan 13y agoThere have been Linux kernel security bugs where a NULL dereference might not result in a crash, because a malicious user-space program had mapped some accessible memory at address zero. The kernel then went on to skip the optimized-out null check, use vtable pointers in this null page and eventually execute arbitrary code in ring 0.