stb_sprintf: potential out-of-bound read from static analysis

Open
#13 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

The question lives in stb_sprintf.h around the indexing into the stbsp__powten table, near the branch that computes e from frac_digits and dg. Read that block to confirm the bounds the reporter describes, then decide between the two proposed fixes: a guard on e or enlarging the stbsp__powten array. Done when the index is provably within the table, or the table is sized to cover it, and the analyzer warning no longer applies.

Written by the indexing model from the issue text.

Description

while running a static analyzer for an application, I stumbled upon this code in stb_sprintf.

https://github.com/nothings/stb/blob/fede005abaf93d9d7f3a679d1999b2db341b360f/stb_sprintf.h#L1765-L1789

Since line 1782 is reached when the if at 1780 is false, the tool assume e can get up to 23, so it would read out of bounds of the stbsp__powten[20] array.

now if one reads the rest of the code it's apparent that that's not possible: frac_digits is at most 23 entering the first if, dg can be anything between 10 and 20 so e will be in the range 3-13.

Anyway, I think it would be a good defensive measure to either replace line 180 with:

 if ((stbsp__uint32)e >= 20) 

or define a stbsp__powten[23]

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.