News for hackers. Stuff that matters.
Re:Defensive Programming by lmm on Wednesday December 23, 2020 @07:33PM (#25523961)
> This way, if anyone actually tries to use that pointer, it will crash the system (hardfault), instead of potentially using corrupted memory, which IMO is much worse.

Oh you sweet summer child. Null pointer deference is undefined behaviour, the compiler will happily turn it into corrupting memory under the right circumstances.

Re:Defensive Programming by tyhoff on Wednesday December 23, 2020 @08:01PM (#25524125)
> Oh you sweet summer child

Should have clarified. On the embedded systems I work on, generally Cortex-M4, you set an MPU region on address 0x0 and it turns into a hardfault, so it's pretty easy to catch.

Re:Defensive Programming by lmm on Wednesday December 23, 2020 @10:14PM (#25524744)
If you were writing assembly that would be reliable, but in C it isn't. The compiler knows that NULL dereferences are undefined behaviour and so won't necessarily compile them into memory accesses.
Re:Defensive Programming by btrask on Thursday December 24, 2020 @04:38AM (#25526192)
Yeah, in C you need to use assertions (or simple checks) for things that might be null.

That said the compiler isn't infinitely smart (thank god) and complex null derefs will "safely" make it to runtime. (What a sad world we're in.)

Re:Defensive Programming by saagarjha on Wednesday December 23, 2020 @08:20PM (#25524239)
A good optimizing compiler can remove the dereference entirely if it notices, unfortunately.
Re:Defensive Programming by sigjuice on Wednesday December 23, 2020 @08:23PM (#25524257)
How big is this MPU region? Pointers to large structs can easily allow access past the protected region.
Re:Defensive Programming by elygre on Thursday December 24, 2020 @12:58PM (#25529725)
It is undefined by the standard, a compiler is thus free to make null pointer references into hard faults. You need to know your tool chain and your environment.