[SECURITY] stbsp_snprintf with count=0 writes null terminator before buffer

Open Beginner friendly
#4 0 comments 0 reactions 0 assignees View on GitHub

@w4ffl35 is already working on this.

Since Oct 26, 2026.

  • #12 by @w4ffl35 — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c
Domain
security

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

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

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.