News for hackers. Stuff that matters.
Re:Show HN: Progressbar - A C library for displaying command... by danieltillett on Saturday August 29, 2015 @01:26AM (#10139172)
If you have run out of memory there is not much you can really do except crash. Even logging is a problem. Better to just crash and deal with it.

I was under the impression that compiler optimisation often removed any NULL checks anyway [1].

1. http://blog.llvm.org/2011/05/what-every-c-programmer-should-...

Re:Show HN: Progressbar - A C library for displaying command... by AndyKelley on Saturday August 29, 2015 @01:37AM (#10139189)
> I was under the impression that compiler optimisation often removed any NULL checks anyway

Goodness.

No, you are misunderstanding the example. The optimizer did not introduce a bug by removing a NULL check. The code had a bug because it dereferenced the pointer before checking for NULL.

Re:Show HN: Progressbar - A C library for displaying command... by danieltillett on Saturday August 29, 2015 @01:59AM (#10139234)
Yes, but they were making the point that thinking you are checking for a null pointer may not always hold especially once inlining happens.

Are there any modern platforms where dereferncing a NULL pointer will not cause a crash?

Edit. Answering my own question it looks like ARM is one [1].

1. https://www.securecoding.cert.org/confluence/display/c/EXP34...

Re:Show HN: Progressbar - A C library for displaying command... by AndyKelley on Saturday August 29, 2015 @02:31AM (#10139286)
> thinking you are checking for a null pointer may not always hold especially once inlining happens

Incorrect. The example code did not check for a null pointer. It dereferenced a pointer, which implies that the pointer will never be null, and then it pointlessly checked for null on something that could not have been null. The optimizer in fact made the code more clear and easier to read, while maintaining correctness.

Re:Show HN: Progressbar - A C library for displaying command... by danieltillett on Saturday August 29, 2015 @02:52AM (#10139318)
Quoting from the reference:

"While this is intentionally a simple and contrived example, this sort of thing happens all the time with inlining: inlining a function often exposes a number of secondary optimization opportunities. This means that if the optimizer decides to inline a function, a variety of local optimizations can kick in, which change the behavior of the code. This is both perfectly valid according to the standard, and important for performance in practice.”

In practice derefencing a NULL pointer on any x86 platform will always cause a crash (defined undefined behaviour I guess), but you can’t assume this on other platforms (ARM seems to be the big one).

I have to say before today I had assumed dereferncing a NULL pointer would always cause a segfault. It is always good to learn something new.

Re:Show HN: Progressbar - A C library for displaying command... by sherjilozair on Saturday August 29, 2015 @02:28AM (#10139276)
You can't be further from the truth. A vast magntitude of bugs are due to the system running as if nothing has happened operating on garbage data, and giving you garbage results. If your system is robust to garbage data, say neural network training, good luck finding the bug then.
Re:Show HN: Progressbar - A C library for displaying command... by danieltillett on Saturday August 29, 2015 @03:00AM (#10139333)
But what do you do in practice if you have run out of memory?
Re:Show HN: Progressbar - A C library for displaying command... by davvid on Saturday August 29, 2015 @03:51AM (#10139394)
One approach is to free memory, retry, and hope for the best.

e.g. https://github.com/git/git/blob/master/wrapper.c#L63

Re:Show HN: Progressbar - A C library for displaying command... by dalke on Saturday August 29, 2015 @04:33AM (#10139444)
Another is to pre-allocate enough reserve memory to be able to save and exit cleanly. When malloc fails, switch to the reserve memory and either shut down nicely, or post a dire warning to the user that the end is neigh and to "SAVE AND EXIT IMMEDIATELY".