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

Invalid output when both the 0 flag and a precision appear #244

Closed
GabrielRavier opened this issue Mar 22, 2023 · 3 comments
Closed

Invalid output when both the 0 flag and a precision appear #244

GabrielRavier opened this issue Mar 22, 2023 · 3 comments

Comments

@GabrielRavier
Copy link

With the following code:

#include <stdio.h>

#ifdef USE_NANOPRINTF
#define NANOPRINTF_IMPLEMENTATION
#define NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS 1
#include "nanoprintf.h"
#define snprintf npf_snprintf
#endif

int main()
{
    char output[100];
    snprintf(output, sizeof(output), "%02.1d", 0);
    printf("%s\n", output);
}

when compiled without USE_NANOPRINTF, I get an output of 0, whereas when using nanoprintf, 00 is output:

$ gcc test.c -o default
$ gcc -DUSE_NANOPRINTF test.c -o nanoprintf
$ ./default; ./nanoprintf 
 0
00

Nanoprintf's behavior is invalid because the C standard states that:

For d, i, o, u, x, and X conversions, if a precision is specified, the 0 flag is ignored.

The current trunk version of nanoprintf fails to do so.

@charlesnicholson
Copy link
Owner

Thanks for the report! I'll investigate this ASAP, sorry!

@charlesnicholson
Copy link
Owner

With #245 and your test driver, I get:

❯ gcc test.c -o default
❯ gcc -DUSE_NANOPRINTF test.c -o npf_test
❯ ./default;./npf_test
 0
 0

I'll cut a new release for this after I merge it.

@charlesnicholson
Copy link
Owner

Released v0.3.4 with this fix.
https://github.com/charlesnicholson/nanoprintf/releases/tag/v0.3.4

Thanks again! I even had it as a requirement in the implementation comments, but didn't adhere to it. Sigh.

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

2 participants