all printf variants: fix argument type handling for %c and %lc
authorRich Felker <dalias@aerifal.cx>
Mon, 12 Jun 2023 23:56:08 +0000 (19:56 -0400)
committerRich Felker <dalias@aerifal.cx>
Mon, 12 Jun 2023 23:56:08 +0000 (19:56 -0400)
%c takes an argument of type int, not char, and %lc/%C takes an
argument of type wint_t (unsigned), not int.

for most cases, this makes no practical difference, but since wide
printf variants convert narrow %c format specifiers via btowc,
interpreting the promoted-to-int unsigned char value passed in as a
(signed, on most archs) char causes 255 to get collapsed to EOF and
interpreted as such by btowc.

this is only relevant in the byte-based C locale, so prior to commit
f22a9edaf8a6f2ca1d314d18b3785558279a5c03, there was no observable
distinction in behavior. for UTF-8, all bytes which might be negative
when interpreted as char are encoding errors when used with %c/btowc.

src/stdio/vfprintf.c
src/stdio/vfwprintf.c

index a712d80..c551ccf 100644 (file)
@@ -52,7 +52,7 @@ static const unsigned char states[]['z'-'A'+1] = {
                S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT,
                S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
                S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
-               S('c') = CHAR, S('C') = INT,
+               S('c') = INT, S('C') = UINT,
                S('s') = PTR, S('S') = PTR, S('p') = UIPTR, S('n') = PTR,
                S('m') = NOARG,
                S('l') = LPRE, S('h') = HPRE, S('L') = BIGLPRE,
@@ -62,7 +62,7 @@ static const unsigned char states[]['z'-'A'+1] = {
                S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG,
                S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
                S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
-               S('c') = INT, S('s') = PTR, S('n') = PTR,
+               S('c') = UINT, S('s') = PTR, S('n') = PTR,
                S('l') = LLPRE,
        }, { /* 2: ll-prefixed */
                S('d') = LLONG, S('i') = LLONG,
index 5369770..59d5471 100644 (file)
@@ -45,7 +45,7 @@ static const unsigned char states[]['z'-'A'+1] = {
                S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT,
                S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
                S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
-               S('c') = CHAR, S('C') = INT,
+               S('c') = INT, S('C') = UINT,
                S('s') = PTR, S('S') = PTR, S('p') = UIPTR, S('n') = PTR,
                S('m') = NOARG,
                S('l') = LPRE, S('h') = HPRE, S('L') = BIGLPRE,
@@ -55,7 +55,7 @@ static const unsigned char states[]['z'-'A'+1] = {
                S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG,
                S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
                S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
-               S('c') = INT, S('s') = PTR, S('n') = PTR,
+               S('c') = UINT, S('s') = PTR, S('n') = PTR,
                S('l') = LLPRE,
        }, { /* 2: ll-prefixed */
                S('d') = LLONG, S('i') = LLONG,