3 ms·
What people forget is that, if the compiler didn't delete the null pointer checks and executed the semantics exactly as the programmer wrote them... the program
by jcranmer 7y ago
What people forget is that, if the compiler didn't delete the null pointer checks and executed the semantics exactly as the programmer wrote them... the program is guaranteed to crash.
Most of the complaints of undefined behavior are actually people who write code with nonsense semantics, and are then surprised to find that the compiler comes out with a different nonsense semantics than what they intended.
- lmm 7y agoFailure semantics are just as important as happy-path semantics. Good languages are fail-stop. The case I'm thinking of was some linux kernel code that looked roughly like: void checkBeforeDangerousThing(SOMETHING* detailsPtr) { SOMETHING details = *detailsPtr; if(detailsPtr == null) return; if(!isValid(details)) return; doDangerousThing(); } The intended semantics of that are pretty clear. There are two defensible implementations for this code if detailsPtr is null (hence why it was left undefined in the C standard): either it should return cleanly without calling doDangerousThing, or it should trap on the invalid null pointer dereference (possibly asynchronously). What no reasonable language community would consider to be a reasonable implementation - what only an adversarial reading of the standard reveals is even an option - is to execute doDangerousThing() when passed a null pointer.