[sprintf] Wrong handling of integer types smaller than int
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 45/100
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
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from nothings/stb_sprintf
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
nothings/stb_sprintf#10 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
nothings/stb_sprintf#8 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
nothings/stb_sprintf#5 ·
-
[SECURITY] stbsp_snprintf with count=0 writes null terminator before bufferPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
nothings/stb_sprintf#4 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
nothings/stb_sprintf#20 ·
All issues in nothings/stb_sprintf
Similar issues
-
bug severity: medium
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
AlessandroSangiuliano/Uros#1014 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
OpenXiangShan/NEMU#1314 ·
Maintainers usually reply within 1 day
-
`get_options()` repeats the first byte when hex-encoding octet stringsPossibly taken @rue19 claimed this today. Open
Difficulty 1/5 Under an hour Newbie friendliness 85/100
OpenPrinting/cups#1830 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
micropython/micropython#20020 ·
Maintainers usually reply within 1 day