Hacker News new | ask | show | jobs
by Xirdus 28 days ago
Maybe your compiler isn't, but GCC is designed to summon every nasal demon it can. It especially loves to elide null pointer checks. There was a PR that almost made it to Linux mainline that did some weird stuff in kernel space where memory mapped to address 0 is valid and contains important stuff. It got a pointer, read data from it, then checked if pointer is null before giving root permissions for some operation. Reading the code, everything seems alright. Even stepping through it with debugger shows that it works. But when compiled with -O2 or higher, GCC removed the null check and now everybody got root permissions unconditionally - because reading from null pointer is UB regardless of circumstances, and GCC treats UB as dead code even if it is reachable.
2 comments

> GCC is designed to summon every nasal demon it can.

Which is zero, to be clear. There's not even a standard USB device class for a nasal demon releaser

A null check elision is not a demon up my nose - it's a null check elision.

Sure, and char[] is no string because you can't tie anything with it. And don't get me started on the insects that allegedly live inside digital source files - that's an absurd concept, no self respecting computer specialist should be talking like that.
Having made devices for other human holes, you can just use the standardized HID class. It can be adapted for nasal demons incoming and outgoing.
Wait, so if you're running on embedded, you're not allowed to store anything at address 0, requiring you to waste that little bit of RAM?

That's... a bizarre choice, considering how religious C is about not making any assumptions about what a machine can and cannot do, even going to the trouble of working with non-IEEE floats and int types that are not a multiple of 8 bits.

No. IIRC, you’re just not allowed to use a constant expression evaluating to 0 as a pointer to construct a non-null pointer to address zero.

Runtime pointer arithmetic resulting in a zero-valued pointer, is not a null pointer m, though it is also nonportable and could have the same effect (but hopefully not nasal demons, as I believe this falls under some catagory of nonportable behavior other than “undefined”).

There’s no requirement that the in-memory representation of a null pointer be all-zeroes, only that constant 0 represents a null pointer in source code.

Regardless, I agree that it’s a bizarre choice.

Unfortunately yes. ISO/IEC 9899:1999 footnote 83: "Among the invalid values for dereferencing a pointer by the unary * operator are a null pointer".
A null pointer doesn't need to map to the hardware address at 0x0. A null pointer constant of 0 in C code could have a different hardware address than 0x0, or be represented by a special bit independent of the other representation bits, and such machines have existed and, I think, still exist.

However, to make a pointer to a hardware address at 0x0 in C source code (for either dereferencing or comparison) requires using a special routine, intrinsic, or similar extension so the compiler doesn't track it as a null pointer during static analysis.