[sprintf] Wrong handling of integer types smaller than int

Open
#32 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

The bug is in how the library narrows integer arguments for the h and hh length modifiers, so start with the format-specifier handling in the single header of the stb_sprintf repo. Compare outputs against the printf examples in the issue, and the C standard text quoted there (§7.23.6.1), to see what a conforming result looks like. Done means %hhi, %hi, %hu and %hhu with out-of-range int values print the value converted to the narrower type, and %c with a large value prints the low byte.

Written by the indexing model from the issue text.

Description

The C23 standard (here the latest public draft: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf), as well as earlier standards, require that integer types smaller than int are cast to the appropriate type by the library itself.

Smaller-than-int arguments are automatically promoted when passed as variadic arguments. However, the caller might also specify a small type and yet pass a full-int type as argument --eg printf("%hu", 0xFFFFFFFFu).
This is a weird corner case, but STB is not conforming to the standard, which explicitly states (§7.23.6.1):

hh Specifies that a following b, B, d, i, o, u, x, or X conversion specifier applies to a signed char or unsigned char argument (the argument will have been promoted according to the integer promotions, but its value shall be converted to signed char or unsigned char before printing)

And similar statements for 'h', as well as the new 'w', 'wf', and for %n.

See the output of STB vs standard-conforming implementations:

printf("%c", (int)('a' + (UINT_MAX << 8)));    // "0"     " vs "a"
printf("%hhi", INT_MIN);                       // "2357808" vs "0"
printf("%hhi", INT_MAX);                       // "2357808" vs "-1"
printf("%hhu", INT_MIN);                       // "2357808" vs "0"
printf("%hhu", UINT_MAX);                      // "2357808" vs "65535"
printf("%hi", INT_MIN);                        // "2357808" vs "0"
printf("%hi", INT_MAX);                        // "2357808" vs "-1"
printf("%hu", INT_MIN);                        // "2357808" vs "0"
printf("%hu", UINT_MAX);                       // "2357808" vs "65535"
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.