printf icon indicating copy to clipboard operation
printf copied to clipboard

printf("%s", NULL) - crash

Open mad-rain opened this issue 6 years ago • 4 comments

Hi!

Some placeholder should be used for NULL %s argument to prevent printf from crash:

      case 's' : {
        const char* p = va_arg(va, char*);
        if (!p) p = "(null)"; // please add this

mad-rain avatar Oct 10 '19 05:10 mad-rain

I just came here to add just that!

    const char* p = va_arg(va, char*);
    p = (p == NULL) ? "(null)" : p;

noomio avatar Oct 17 '19 22:10 noomio

This should be an optional behaviour as in many embedded situations address zero is a perfectly valid one.

dev-zzo avatar Apr 22 '20 08:04 dev-zzo

@dev-zzo : What about %p, then? Should that also not have an option to print 0 as 0 rather than (nil) on such platforms?

eyalroz avatar Jun 30 '21 17:06 eyalroz

So, this issue is now fixed on my fork. @dev-zzo , @noomio , @mpaland Please have a look and voice your opinion about the behavior with null/0 pointers.

Also, @mpaland : Can you explain the code's "partiality" to the use of _out_rev? That is, what's the benefit of printing characters backwards like that? I used it too in the changes I made for this issue, but I was just following the pattern.

eyalroz avatar Jun 30 '21 21:06 eyalroz