News for hackers. Stuff that matters.
Re:When you see a Heisenbug in C, change your compiler's opt... by userbinator on Saturday March 01, 2014 @09:00PM (#7327304)
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.

Re:When you see a Heisenbug in C, change your compiler's opt... by chowells on Saturday March 01, 2014 @09:14PM (#7327359)
If 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-...

Please read that article, and the rest in the series. Undefined behavior is far more pervasive than you think.

Re:When you see a Heisenbug in C, change your compiler's opt... by theseoafs on Sunday March 02, 2014 @03:09AM (#7328232)
Why 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.
Re:When you see a Heisenbug in C, change your compiler's opt... by userbinator on Sunday March 02, 2014 @12:18AM (#7327821)
> 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.

Re:When you see a Heisenbug in C, change your compiler's opt... by mansr on Sunday March 02, 2014 @04:24PM (#7330831)
GCC can produce all of those warnings.
Re:When you see a Heisenbug in C, change your compiler's opt... by bdonlan on Sunday March 02, 2014 @03:16AM (#7328250)
There 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.
Re:When you see a Heisenbug in C, change your compiler's opt... by azakai on Saturday March 01, 2014 @10:44PM (#7327609)
> 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.