Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

printf("%s", NULL) - crash #63

Open
mad-rain opened this issue Oct 10, 2019 · 4 comments
Open

printf("%s", NULL) - crash #63

mad-rain opened this issue Oct 10, 2019 · 4 comments
Assignees

Comments

@mad-rain
Copy link

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
@noomio
Copy link

noomio commented Oct 17, 2019

I just came here to add just that!

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

@mpaland mpaland self-assigned this Jan 1, 2020
@dev-zzo
Copy link

dev-zzo commented Apr 22, 2020

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

@eyalroz
Copy link

eyalroz commented Jun 30, 2021

@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 referenced this issue in eyalroz/printf Jun 30, 2021
…ones.

* "splits" behavior of pointers and hash-marked base-16 values w.r.t the `0x` prefix (although you won't see the difference unless you un-special-case the handling of 0-valued pointer).
* Adds a CMake option to choose between zero pointers being interprted as null and invalid (and formatted as `(nil)`), and them being handled just like any other valid pointer, including dereferencing for `%s`.
* When 0-valued pointers are assumed to be invalid, passing a null pointer for an `%s` will result in `(null)`.
* Added some relevant assertions involving null/0 pointers as `%s` arguments, for when we _are_ giving 0 pointers special treatment (the default case).
@eyalroz
Copy link

eyalroz commented Jun 30, 2021

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 referenced this issue in eyalroz/printf Jul 1, 2021
…epresentation is no all-0-bits.

* "splits" behavior of pointers and hash-marked base-16 values w.r.t the `0x` prefix (although you won't see the difference unless you un-special-case the handling of 0-valued pointer).
* Passing a null pointer for an `%s` now results in `(null)`.
* Added some relevant test suite assertions involving null/0 pointers as `%s` arguments.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants