Wrong handling of "%#o" #1794

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

Nobody has claimed this yet.

Assessment

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

Research direction

Start in stb_sprintf.h around line 1029, where the issue identifies the precision check to change. Read the surrounding octal formatting logic and compare its output with the standard for printf("%#.0o", 0). Done means the output is 0 for that case while existing formatting behavior remains intact.

Written by the indexing model from the issue text.

Description

(moved from https://github.com/nothings/stb/issues/1794)

The standard says:

# The result is converted to an "alternative form". For o conversion, it increases the precision, if and only if necessary, to force the first digit of the result to be a zero (if the value and precision are both 0, a single 0 is printed)

STB vs standard-conforming:

printf("%#.0o", 0); // "" vs "0"

Fix: change line 1029
https://github.com/nothings/stb/blob/f0569113c93ad095470c54bf34a17b36646bbbb5/stb_sprintf.h#L1025-L1034
from:

if (pr == 0)

to:

if (pr == 0 && !(f[0] == 'o' && (fl & STBSP__LEADING_0X)))
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.