[sprintf] Overrun of the internal buffer, due to large precision
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- Half a day
- Newbie friendliness
- 45/100
Research direction
The overrun is in the integer-to-string code in stb_sprintf.h: the radix loop near line 1036 (b/o/x/p) and the decimal loop near line 1087 (u/d/i), both writing backwards into the STBSP__NUMSZ num buffer. Start by reproducing with printf("%0.513x", 1) under AddressSanitizer, then compare the exit condition in the radix loop with the decimal one. Done means no out-of-bounds write for large precision, and printf("%'.20x", 1) producing exactly 20 characters.
Written by the indexing model from the issue text.
Description
The library uses an internal 'num' buffer (of size STBSP__NUMSZ, 512) for converting integers/floats to string.
This is sized so that the largest integer (64-bit with "%b" and STBSP__TRIPLET_COMMA) can fit into it, as well as the largest float (~= 1e+308 with "%f" and STBSP__TRIPLET_COMMA).
The padding due to width and precision does not overrun the buffer, since it is handled separately.
However, the code around line 1036 does use the internal buffer; this code handles the conversion to string of unsigned types, which includes STBSP__TRIPLET_COMMA and precision, but not width. So, a long padding due to width is not handled here, and causes no problems, but a long padding due to precision does create problems. Recall that for integers, "precision" means the minimum number of digits to be printed, even though they would otherwise be superfluous.
So, printf("%.10i", 1) prints "0000000001" rather than "1".
A buffer overrun can thus be triggered by a call like:
printf("%0.513x", 1)
In this case, the 'num' buffer is filled from the end, proceeding backwards, so the overrun is towards the local variables declared after it.
Notice that a similar loop exists around line 1087 (this is for u/d, while the one above is for b/o/x/p).
This one, however, causes no overrun. So, the following is ok:
printf("%0.513i", 1)
So, the trailing zeros and commas are indeed handled properly outside the buffer; it's just that the loop at line 1036 has a faulty exit condition.
Fix: change line 1039
https://github.com/nothings/stb/blob/f0569113c93ad095470c54bf34a17b36646bbbb5/stb_sprintf.h#L1035-L1048
from:
if (!((n64) || ((stbsp__int32)((num + STBSP__NUMSZ) - s) < pr)))
to
if (!n64)
And line 1162
https://github.com/nothings/stb/blob/f0569113c93ad095470c54bf34a17b36646bbbb5/stb_sprintf.h#L1156-L1165
to:
} else if(pr == 0) {
And also add this check:
if ((fl & STBSP__LEADINGZERO) && pr > 0) {
fl &= ~STBSP__LEADINGZERO;
}
on the "radixnum:" (for b o x p) and the "convert to string" (for u i d) code paths.
Also notice that the current library has a bug regarding the precision, when STBSP__TRIPLET_COMMA is used:
printf("%'.20x", 1); "0,0000,0000,0000,0001" [21 chars instead of 20]
printf("%'0.20x", 1); "0,0000,0000,0000,0001" [21 chars instead of 20]
The provided fix solves this problem too, producing:
printf("%'.20x", 1); ",0000,0000,0000,0001"
printf("%'0.20x", 1); ",0000,0000,0000,0001"
- 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