[sprintf] Wrong handling of integer types smaller than int

Aperta
#32 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
c
Ambito
tooling

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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"
Lingua principale
C
Stelle
18
Fork
5
Merge medio
1m
PR unite (30g)
3

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di nothings/stb_sprintf

Tutte le issue di nothings/stb_sprintf

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.