4 ms·
There is no reason why printf has to be the sacrificial function that blows up on null. Of course not. Every function which has undefined behaviour upon being
by cperciva 11y ago
There is no reason why printf has to be the sacrificial function that blows up on null.
Of course not. Every function which has undefined behaviour upon being passed NULL should blow up on NULL.
In general, anything which can crash should crash, because that's the fastest way to make sure that bugs get found.
- kazinator 11y agoprintf doesn't have undefined behavior on being passed null. printf("%p\n", (void *) 0); // not undefined It's the specific conversion specifier %s which doesn't have defined behavior. > anything which can crash should crash __asm__ __volatile__ can crash. It's undefined behavior.
- dbaupp 11y agoYou're 100% pedantically correct, passing a NULL pointer as the corresponding argument to %p is fine... But that's really missing the point, since we're discussing %s and the only sensible general interpretation of this discussion presumably includes a "(when the other arguments mean the NULL is actually UB)" on everything. (E.g. the assertion for non-NULL-ity would go on the branch of printf that handles %s.)