[sprintf] Wrong handling of " " flag for unsigned specifiers

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

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
c
Domain
tooling

Research direction

The bug is in stb_sprintf.h, where the space flag is parsed around lines 1056-1057 and the STBSP__LEADINGSPACE flag is set. Read how unsigned conversions reuse that flag, then compare the output with the system printf for %- 10u and % 010u. Done means unsigned specifiers ignore the space flag and match the standard output, with the change verified against those two cases.

Written by the indexing model from the issue text.

Description

The standard says:

space If the first character of a signed conversion is not a sign, or if a signed conversion results in no characters, a space is prefixed to the result.

This seems to mean that unsigned specifiers should ignore the ' ' flag. This is what other implementations do.

STB vs standard-conforming:

printf("%- 10u", 1); // " 1        " vs "1         "
printf("% 010u", 1); // " 000000001" vs "0000000001"

Fix: in between lines 1056-1057:
https://github.com/nothings/stb/blob/f0569113c93ad095470c54bf34a17b36646bbbb5/stb_sprintf.h#L1056-L1057
clear the STBSP__LEADINGSPACE flag:

fl &= ~STBSP__LEADINGSPACE;
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.