[sprintf] "Wrong" output for "%'" (triplet_comma) with width
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- Half a day
- Newbie friendliness
- 50/100
Research direction
The bug is in the %' (triplet comma) handling in stb_sprintf.h, around lines 1207-1236, where leading zeros are emitted and the STBSP__TRIPLET_COMMA flag places separators. Start by reading that block, then compile a small test calling printf("%'08i", 1) against the header. Done means the leading separator is no longer printed; the issue proposes "0000,001" but offers alternatives, so confirm the intended output with a maintainer first.
Written by the indexing model from the issue text.
Description
The ' flag is a non-standard extension introducing comma thousand-separators in integer/float values (and also in binary/octal/hex values, with a different "periodicity").
Since it's not standard, STB can behave in any way; however, the current behavior is "surprising": when the width is such that the very first character should be a separator, a separator is indeed printed, which is weird because it is not separating anything, but rather leading the number.
For instance:
printf("%'08i", 1);
produces
",000,001"
I think it would less surprising to just pad with a zero rather than a separator, in this case:
"0000,001"
similarly to what is done in other contexts, eg "1000,000" is sometimes used rather than "1,000,000".
The alternatives would be to use space-padding (" 000,001"), which is surprising as well, since the user wanted 0-padding, or to reduce the width by 1 ("000,001"), which is surprising as well, since the user did specify the width, and the minimum width is something that works consistently across all specifiers and flags.
Fix: substitute lines 1207-1236:
https://github.com/nothings/stb/blob/f0569113c93ad095470c54bf34a17b36646bbbb5/stb_sprintf.h#L1207-L1236
with the following (this introduces some logic with 'n'):
// copy leading zeros
c = cs >> 24;
cs &= 0xffffff;
cs = (fl & STBSP__TRIPLET_COMMA) ? ((stbsp__uint32)(c - ((pr + cs) % (c + 1)))) : 0;
n = 1; // 1: we are at the beginning, so print '0' instead of comma
while (pr > 0) {
stbsp__cb_buf_clamp(i, pr);
pr -= i;
if ((fl & STBSP__TRIPLET_COMMA) == 0) {
while (i) {
if ((((stbsp__uintptr)bf) & 3) == 0)
break;
*bf++ = '0';
--i;
}
while (i >= 4) {
*(stbsp__uint32 *)bf = 0x30303030;
bf += 4;
i -= 4;
}
}
while (i) {
if ((fl & STBSP__TRIPLET_COMMA) && (cs++ == c)) {
cs = 0;
*bf++ = n ? '0' : stbsp__comma;
} else
*bf++ = '0';
n = 0;
--i;
}
stbsp__chk_cb_buf(1);
}
- Dominant language
- C
- Stars
- 18
- Forks
- 5
- Avg merge
- 1m
- Merged PRs (30d)
- 3
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from nothings/stb_sprintf
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
nothings/stb_sprintf#10 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
nothings/stb_sprintf#8 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
nothings/stb_sprintf#5 ·
-
[SECURITY] stbsp_snprintf with count=0 writes null terminator before bufferPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
nothings/stb_sprintf#4 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
nothings/stb_sprintf#20 ·
All issues in nothings/stb_sprintf
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
Maintainers usually reply within 5 days
-
Difficulty 1/5 Under an hour Newbie friendliness 84/100
-
bug severity: medium
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
AlessandroSangiuliano/Uros#1014 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
OpenXiangShan/NEMU#1314 ·
Maintainers usually reply within 1 day