[sprintf] Wrong formatting of nan/inf with %a %A
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 58/100
Research direction
The bug is in the case 'a' path of stb_sprintf.h, just after stbsp__real_to_parts() converts the double, so start there. The issue gives a suggested check on the unbiased exponent that routes NaN and infinity to the same output as %f. Build a small program using the make_nan macros and the 0/0.0, +1/0.0 and -1/0.0 cases, and compare stb's output with the C library's printf. Done means %a and %A print nan and inf with the sign and width behaviour the standard requires, including the padded cases.
Written by the indexing model from the issue text.
Description
Regarding %a %A, the standard says:
A double argument representing an infinity or NaN is converted in the style of an f or F
conversion specifier.
STB vs standard-conforming implementation (notice also the wrong upper/lower case, already reported):
printf("%a", STB_POS_QNAN); // "0x1.800000p+1024" vs "nan"
printf("%a", STB_NEG_QNAN); // "-0x1.800000p+1024" vs "-nan"
printf("%a", STB_POS_SNAN); // "0x1.000000p+1024"" vs "nan"
printf("%a", STB_NEG_SNAN); // "-0x1.000000p+1024" vs "-nan"
printf("%a", 0/0.0); // "-0x1.800000p+1024" vs "-nan"
printf("%a", +1/0.0); // "0x1.000000p+1024" vs "inf"
printf("%a", -1/0.0); // "-0x1.000000p+1024" vs "-inf"
printf("%020a", STB_POS_QNAN); // "0x00001.800000p+1024" vs " nan"
printf("%020a", STB_NEG_QNAN); // "-0x0001.800000p+1024" vs " -nan"
printf("%020a", STB_POS_SNAN); // "0x00001.000000p+1024" vs " nan"
printf("%020a", STB_NEG_SNAN); // "-0x0001.000000p+1024" vs " -nan"
printf("%020a", 0/0.0); // "-0x0001.800000p+1024" vs " -nan"
printf("%020a", +1/0.0); // "0x00001.000000p+1024" vs " inf"
printf("%020a", -1/0.0); // "-0x0001.000000p+1024" vs " -inf"
printf("%20a", STB_POS_QNAN); // " 0x1.800000p+1024" vs " nan"
printf("%20a", STB_NEG_QNAN); // " -0x1.800000p+1024" vs " -nan"
printf("%20a", STB_POS_SNAN); // " 0x1.000000p+1024" vs " nan"
printf("%20a", STB_NEG_SNAN); // " -0x1.000000p+1024" vs " -nan"
printf("%20a", 0/0.0); // " -0x1.800000p+1024" vs " -nan"
printf("%20a", +1/0.0); // " 0x1.000000p+1024" vs " inf"
printf("%20a", -1/0.0); // " -0x1.000000p+1024" vs " -inf"
Reproducible nans were 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 the case 'a' code path, right after the conversion using stbsp__real_to_parts(), add the code below.
This is around here:
https://github.com/nothings/stb/blob/f0569113c93ad095470c54bf34a17b36646bbbb5/stb_sprintf.h#L639-L651
The code to add:
if (dp == 2047 - 1023) { // dp is the unbiased exponent; this checks if the number is nan or inf
// read the double into a string
if (stbsp__real_to_str(&sn, &ln, num, &dp, fv, 0, (f[0] == 'A')))
fl |= STBSP__NEGATIVE;
goto dofloatfromg; // nan/inf are output just like for %f
}
- 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