[sprintf] Wrong formatting of nan/inf with leading 0
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 70/100
Research direction
The bug is in the float formatting paths of the single-header sprintf implementation, at the doexpfromg: and dofloatfromg: labels. Build a small test program that prints NaN and infinity values with %f and %06f, and compare the output with the C standard library. Done means that NaN and infinity print in lowercase, without a leading zero pad, and that the sign is handled as the issue describes.
Written by the indexing model from the issue text.
Description
In a footnote, the standard says:
When applied to infinite and NaN values, the -, +, and space flag characters have their usual meaning; the # and 0 flag characters have no effect.
STB vs standard-conforming implementation (notice also the wrong upper/lower case, already reported):
printf("%f", STB_POS_QNAN); // "NaN" vs "nan"
printf("%f", STB_NEG_QNAN); // "-NaN" vs "-nan"
printf("%f", STB_POS_SNAN); // "NaN" vs "nan"
printf("%f", STB_NEG_SNAN); // "-NaN" vs "-nan"
printf("%f", 0/0.0); // "-NaN" vs "-nan" or "nan" (the sign of nans is not guaranteed)
printf("%f", +1/0.0); // "Inf" vs "inf"
printf("%f", -1/0.0); // "-Inf" vs "-inf"
printf("%06f", STB_POS_QNAN); // "000NaN" vs " nan"
printf("%06f", STB_NEG_QNAN); // "-00NaN" vs " -nan"
printf("%06f", STB_POS_SNAN); // "000NaN" vs " nan"
printf("%06f", STB_NEG_SNAN); // "-00NaN" vs " -nan"
printf("%06f", 0/0.0); // "-00NaN" vs " -nan"
printf("%06f", +1/0.0); // "000Inf" vs " inf"
printf("%06f", -1/0.0); // "-00Inf" vs " -inf"
Reproducible nans for testing can be generated this way:
double make_nan(int sign, int signaling)
{
unsigned long long v = ((sign ? 1ull : 0ull) << 63)
| (0x7FFull << 52)
| (signaling ? 1ull : (1ull << 51))
;
double d;
memcpy(&d, &v, 8);
return d;
}
#define STB_POS_QNAN make_nan(0, 0)
#define STB_NEG_QNAN make_nan(1, 0)
#define STB_POS_SNAN make_nan(0, 1)
#define STB_NEG_SNAN make_nan(1, 1)
Fix: in both the doexpfromg: and dofloatfromg: code paths, add this:
if (dp == STBSP__SPECIAL) {
fl &= ~STBSP__LEADINGZERO;
s = (char *)sn;
cs = 0;
pr = 0;
goto scopy;
}
- 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