5 ms·
Old school comment... back in the day, there were computers on which dereferencing the null pointer would in fact return zero. And given that a typical linked
by MarkusWandel 3y ago
Old school comment... back in the day, there were computers on which dereferencing the null pointer would in fact return zero. And given that a typical linked list is terminated by a null, mistakes could creep in where "points to a 0" was checked when "pointer is 0" should have been. Needless to say, code like this would crash in mysterious ways when compiled on a machine where location 0, while readable, had nonzero contents. The alternative, getting a segfault because 0 wasn't mapped, is preferable because then you see the problem right away.
Anyhow it's just barely possible that this assignment was a hack to make this sort of code work on a machine where address 0 is (uninitialized) nonzero and can be harmlessly made zero.
- AlotOfReading 3y agoI've actually seen code equivalent to this used for control flow. It was a very old bit of safety-critical code(!) that initialized a bunch of hardware from predefined tables containing pointers to overrides. For table values that weren't overridden (very few by the time I came along), it would access the null pointer to intentionally trap so an interrupt could insert the correct default and restart the instruction. Certain higher ups determined that behavior was a business requirement when I refused to reimplement it on a new platform.
- pavlov 3y agoIBM AIX is a Unix-like system on which dereferencing a null pointer for a read is guaranteed to return zero. But writes will crash. It’s pretty weird.
- twoodfin 3y agoHP-UX binaries also had an attribute that would control this behavior. https://docstore.mik.ua/manuals/hp-ux/en/B2355-60130/chatr_pa.1.html https://docstore.mik.ua/manuals/hp-ux/en/B2355-60130/chatr_p... Wonderful fun when they shipped a Kerberos library that unconditionally dereferenced a potentially null optional pointer-to-struct, relying on the executable to be set in the “null tolerant” mode!
- rsaxvc 3y agoMany(most?) Cortex-M4 systems boot from the reset vector and stack pointer stored in the first 8B of memory, so it's a valid address on a ton of small systems today. You can relocate the vector table and to block it off with the MPU if you have one and memory to spare.
- deleted 3y ago[deleted]