5 ms·
This is nonsense. There is no reason why printf has to be the sacrificial function that blows up on null. Those same "misguided" operating systems run on hardwa
by kazinator 11y ago
This is nonsense. There is no reason why printf has to be the sacrificial function that blows up on null. Those same "misguided" operating systems run on hardware that traps null dereferences via an unmapped page. A predictable, tidy crash is just around the corner if that same is used in the program. On the other hand, if that printf is the only place the pointer is used, then there is no security hole. It's just undefined behavior on ISO paper, and you don't agree with how it is locally defined by the implementation.
How do we know it's a security hole? Maybe some structure has a "char * " name field, and null indicates "anonymous/has no name". It's an ISO C conformance bug to try to print that with %s, but that's a portability issue. If that's the only problem in the program (everywhere else it checks for null), there is no real issue.
POSIX and C libraries are not entirely about allowing null pointers or not. For instance gettimeofday lets you pass a null pointer for the "struct timezone * " second argument, which indicates that you aren't interested in specifying or retrieving it. On the other hand, a null argument is undefined behavior in getgroups, even if the array size is specified as zero (meaning, return me the size of the array actually needed and I will call you again soon with such an array)! A non-null pointer must be passed as a POSIX-conforming eggcorn. According to the logic of this article, if I pass null I'm committing a security hole, and an OS which allows getgroups(0, NULL) is misguidedly sweeping it under the rug.
Null pointers are not some "bogey man". I built a Lisp dialect in whose implementation a C null pointer represents the nil symbol (empty list, false, ...). It prints as "nil", satsifies the symbolp function and so fort. Lexical variables are initialized to nil if not given an initial expression. Some C library printing a null pointer as (null) seems like nothing by comparison.
Be that all as it may, you cannot actually fix this perceived issue by changing from (null) to crash, cold turkey. Unknown numbers of nonportable programs out there (otherwise well-tested programs) which rely on this will just crash.
I just did a "grep -r -F '(null)' /var/log" on a system here and found a few hits. Most of those are probably via the %s conversion, and all that code would have to be fixed if printf were to assert this. And that's just instances confined to logging, not all output everywhere.
- cperciva 11y agoThere 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.)
- rtpg 11y ago>Maybe some structure has a "char * " name field, and null indicates "anonymous/has no name". It's an ISO C conformance bug to try to print that with %s, but that's a portability issue. If that's the only problem in the program (everywhere else it checks for null), there is no real issue. I'll take 'uses of Option/Maybe' for 200 Alex Some might say it's overkill, but if you can have a char* be null, then for most intents and purposes you have two states to deal with on any usage of that value. Just because such a design can exist doesn't mean we should hobble our tools to support it.
- kazinator 11y agoIt could simplify things elsewhere. In Lisp, CAR and CDR accessors take a cons cell or NIL, which isn't a cons cell, and has no such fields. It wasn't always like that; according to the HOPL paper on evolution of Lisp, InterLisp introduced this nicety and it was copied by others. These functions are complicated with that extra state, but programs are simplified. A null "char *" indicating "this object has no name" is better than some special string value doing the same, because it's different from every possible string value.
- rtpg 11y agoI'm not saying a special string value, but a wrapper object with an "existence" bit. You can then assume, if the existence bit is flipped right, that the char* "exists". This is functionally equivalent, but forces existence checks when they're needed. And by removing "null" from your vocabulary, an entire class of errors disappears. With C this is a bit trickier to ensure. But I think the design pattern can still be useful. And it doesn't make code more complicated, but rather it makes the complicated aspects explicit . Instead of testing that name == null, you test that name.exists is False, and if not you use name.value. And you make null a banned word in your codebase. The result is that you _never_ have to ask "is this char* possibly null?". The answer is always "no". So you remove superfluous checks I think this pattern works much better when you have stronger type checking, though...
- dllthomas 11y agoA nullable char pointer is a perfectly sensible implementation of "Maybe String". At least, presuming a non-nullable char pointer is a perfectly sensible implementation of String.