[SECURITY] stbsp_snprintf with count=0 writes null terminator before buffer
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
Research direction
Read stb_sprintf.h around stbsp_vsnprintf lines 1430–1454, then run the attached poc_sprintf.c with AddressSanitizer to confirm the reported out-of-bounds write. Check both count=0 and negative-count behavior; done means the calls no longer write outside the supplied buffer and the regression is covered.
Written by the indexing model from the issue text.
Description
(mirrored issue 1963 from the original stb repo https://github.com/nothings/stb/issues/1963 since it concerns stb_sprintf)
Summary
Analysis performed with the assistance of AI-based code review tools.
When stbsp_snprintf is called with count=0 and a non-NULL buffer, the null terminator is written to buf[-1], causing a heap-buffer-overflow write (or stack-buffer-overflow, depending on the buffer).
Environment
- File:
stb_sprintf.h - Commit: 31c1ad3
- OS: Linux x86_64
- Compiler: gcc 15.2.0
- Test flags:
-fsanitize=address -g -O1
Root cause
In stbsp_vsnprintf (stb_sprintf.h:1430), when count=0 and buf != NULL, the code enters the else branch at line 1440:
if ((count == 0) && !buf) // line 1434: count==0 AND buf==NULL - count-only path
{ ... }
else // line 1440: count==0 but buf!=NULL - BUG
{
c.buf = buf;
c.count = count; // c.count = 0
...
l = (int)(c.buf - buf); // l = 0
if (l >= count) // 0 >= 0 - true
l = count - 1; // l = -1
buf[l] = 0; // buf[-1] = 0 OOB WRITE
}
stbsp__clamp_callback clamps all output to c.count bytes. With c.count=0, nothing is written and c.buf never advances. The null-termination logic then computes l = -1, writing 0 one byte before the buffer.
A related path exists with count < 0: the negative value flows into the initial callback call, producing a different negative offset (e.g., count=-1 gives buf[-2]=0).
Trigger
char *buf = (char*)malloc(8);
stbsp_snprintf(buf, 0, "%d", 42);
// buf[-1] is now 0 (OOB write)
Impact
One-byte out-of-bounds write immediately before a caller-provided buffer, with an unbounded negative offset in the count < 0 case. Calling stbsp_snprintf with count=0 and a non-NULL buffer reaches this code path and causes the one-byte write before the buffer.
ASAN evidence
=================================================================
==243076==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7838551e000f
READ of size 1 at 0x7838551e000f
#0 main poc_sprintf.c:10
0x7838551e000f is located 1 bytes before 8-byte region [0x7838551e0010,0x7838551e0018)
allocated by malloc at poc_sprintf.c:7
Note: ASAN reports READ because the PoC reads buf[-1] to verify the write. The actual write buf[-1] = 0 at stb_sprintf.h:1454 is the bug.
Full log: see attached verification data.
PoC
Attached: poc_sprintf.c
Compile and run:
gcc -fsanitize=address -g -O1 poc_sprintf.c -o poc_sprintf -lm
./poc_sprintf
- 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 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
nothings/stb_sprintf#20 ·
-
Difficulty 2/5 Under an hour Newbie friendliness 68/100
nothings/stb_sprintf#17 ·
All issues in nothings/stb_sprintf
Similar issues
-
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
-
`get_options()` repeats the first byte when hex-encoding octet stringsPossibly taken @rue19 claimed this today. Open
Difficulty 1/5 Under an hour Newbie friendliness 85/100
OpenPrinting/cups#1830 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
micropython/micropython#20020 ·
Maintainers usually reply within 1 day