Index: usr.bin/printf/printf.c =================================================================== RCS file: /home/netbsd/src/usr.bin/printf/printf.c,v retrieving revision 1.50 diff -p -u -r1.50 printf.c --- usr.bin/printf/printf.c 22 Jul 2019 17:34:31 -0000 1.50 +++ usr.bin/printf/printf.c 25 Oct 2020 07:22:52 -0000 @@ -54,6 +54,7 @@ __RCSID("$NetBSD: printf.c,v 1.50 2019/0 #include #include #include +#include #include #include #include @@ -71,7 +72,7 @@ static char *conv_expand(const char *); static char getchr(void); static double getdouble(void); static int getwidth(void); -static intmax_t getintmax(void); +static intmax_t getintmax(bool); static char *getstr(void); static char *mklong(const char *, char); static void check_conversion(const char *, const char *); @@ -320,7 +321,7 @@ main(int argc, char *argv[]) } case 'd': case 'i': { - intmax_t p = getintmax(); + intmax_t p = getintmax(true); char *f = mklong(start, ch); PF(f, p); @@ -332,7 +333,7 @@ main(int argc, char *argv[]) case 'u': case 'x': case 'X': { - uintmax_t p = (uintmax_t)getintmax(); + uintmax_t p = (uintmax_t)getintmax(false); char *f = mklong(start, ch); PF(f, p); @@ -651,7 +652,7 @@ getstr(void) static int getwidth(void) { - unsigned long val; + uintmax_t val; char *s, *ep; s = *gargv; @@ -660,7 +661,7 @@ getwidth(void) gargv++; errno = 0; - val = strtoul(s, &ep, 0); + val = strtoumax(s, &ep, 0); check_conversion(s, ep); /* Arbitrarily 'restrict' field widths to 1Mbyte */ @@ -673,7 +674,7 @@ getwidth(void) } static intmax_t -getintmax(void) +getintmax(bool is_signed) { intmax_t val; char *cp, *ep; @@ -687,7 +688,10 @@ getintmax(void) return *(cp + 1); errno = 0; - val = strtoimax(cp, &ep, 0); + if (is_signed) + val = strtoimax(cp, &ep, 0); + else + val = (intmax_t)strtoumax(cp, &ep, 0); check_conversion(cp, ep); return val; } Index: tests/usr.bin/printf/printf.sh =================================================================== RCS file: /home/netbsd/src/tests/usr.bin/printf/printf.sh,v retrieving revision 1.6 diff -p -u -r1.6 printf.sh --- tests/usr.bin/printf/printf.sh 24 Apr 2020 14:29:19 -0000 1.6 +++ tests/usr.bin/printf/printf.sh 25 Oct 2020 09:56:09 -0000 @@ -445,6 +445,13 @@ d_decimal() expect '-09 ' '%- +4.2d' -9 expect '+09 ' '% +-4.2d' 9 + # numbers whose absolute values exceed INT64_MAX + # XXX no way to obtain sizeof(intmax_t) from portable shell scripts + # for sizeof(intmax_t) == 8 + # expect_fail 9223372036854775807 %d 0xffffc00000000000 + # for sizeof(intmax_t) == 16 + # expect 18446673704965373952 %d 0xffffc00000000000 + expect_fail '0' %d junk expect_fail '123' %d 123kb expect_fail '15' %d 0xfooD @@ -554,6 +561,9 @@ u_unsigned() expect ! '-*' '%+u' -7 + # numbers whose absolute values exceed INT64_MAX + expect 18446673704965373952 %u 0xffffc00000000000 + expect_fail '0' %u junk expect_fail '123' %u 123kb expect_fail '15' %u 0xfooD @@ -675,6 +685,9 @@ x_hex() expect '*fffabc' %x -1348 expect '*ff3502' %x -0xCAFE + # numbers whose absolute values exceed INT64_MAX + expect ffffc00000000000 %x 0xffffc00000000000 + expect_fail '0 1 2' %x%2x%2x junk 1 2 expect_fail '3 1 2' %x%2x%2x 3 1+1 2