Skip to content

[sprintf] Wrong handling of "+ " flags #1788

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

Open
stefano-zanotti-88 opened this issue May 4, 2025 · 0 comments
Open

[sprintf] Wrong handling of "+ " flags #1788

stefano-zanotti-88 opened this issue May 4, 2025 · 0 comments

Comments

@stefano-zanotti-88
Copy link
Contributor

The standard says:

If the space and + flags both appear, the space flag is ignored.

STB vs standard-conforming:

printf("%+ 10i", 1);   // "         1" vs "        +1"
printf("% +10i", 1);   // "         1" vs "        +1"
printf("%+ 10f", 1.0); // "  1.000000" vs " +1.000000"
printf("% +10f", 1.0); // "  1.000000" vs " +1.000000"
printf("%+ 10g", 1.0); // "         1" vs "        +1"
printf("% +10g", 1.0); // "         1" vs "        +1"
printf("%+ 10a", 1.0); // "       0x1.000000p+0" vs "      +0x1.000000p+0"
printf("% +10a", 1.0); // "       0x1.000000p+0" vs "      +0x1.000000p+0"

Fix: the stbsp__lead_sign() function should change the order of its checks: first negative, then plus, then space (now it checks negative, space, plus):

stb/stb_sprintf.h

Lines 294 to 307 in f056911

static void stbsp__lead_sign(stbsp__uint32 fl, char *sign)
{
sign[0] = 0;
if (fl & STBSP__NEGATIVE) {
sign[0] = 1;
sign[1] = '-';
} else if (fl & STBSP__LEADINGSPACE) {
sign[0] = 1;
sign[1] = ' ';
} else if (fl & STBSP__LEADINGPLUS) {
sign[0] = 1;
sign[1] = '+';
}
}

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

1 participant