[sprintf] Overrun of the internal buffer, due to large precision

Open
#18 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
Half a day
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
c
Domain
backend

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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nothings/stb_sprintf

All issues in nothings/stb_sprintf

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.