From 1f446d8434a38afc2c90eca121d1e8b6b9690f9b Mon Sep 17 00:00:00 2001 From: pfg Date: Sat, 10 May 2014 22:27:01 +0000 Subject: prinf: replace use of alloca with variable length array. Use of alloca(3) is discouraged in FreeBSD. Using a VLA is simple and should be more portable. Requested by: jilles --- usr.bin/printf/printf.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'usr.bin/printf') diff --git a/usr.bin/printf/printf.c b/usr.bin/printf/printf.c index ac122f7..055657f 100644 --- a/usr.bin/printf/printf.c +++ b/usr.bin/printf/printf.c @@ -215,13 +215,11 @@ printf_doformat(char *fmt, int *rval) static const char skip1[] = "#'-+ 0"; int fieldwidth, haveprec, havewidth, mod_ldbl, precision; char convch, nextch; - char *start; + char start[strlen(fmt) + 1]; char **fargv; char *dptr; int l; - start = alloca(strlen(fmt) + 1); - dptr = start; *dptr++ = '%'; *dptr = 0; -- cgit v1.1 From a6ebcfc558864aa5250fb99f13caa1450d9dff01 Mon Sep 17 00:00:00 2001 From: pfg Date: Sun, 11 May 2014 17:28:57 +0000 Subject: printf: fix regression after illumos merges. The "bltin/bltin.h" wrappers do not support exit() and attempting to call it will exit sh completely. Note that errx() is acceptable but will always return with status 2. Reported by: jilles (and the testing framework) Fix by: jilles Pointyhat: pfg --- usr.bin/printf/printf.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'usr.bin/printf') diff --git a/usr.bin/printf/printf.c b/usr.bin/printf/printf.c index 055657f..7a7e055 100644 --- a/usr.bin/printf/printf.c +++ b/usr.bin/printf/printf.c @@ -101,6 +101,8 @@ static void usage(void); static const char digits[] = "0123456789"; +static char end_fmt[1]; + static int myargc; static char **myargv; static char **gargv; @@ -171,11 +173,11 @@ main(int argc, char *argv[]) fmt += 2; } else { fmt = printf_doformat(fmt, &rval); - if (fmt == NULL) { + if (fmt == NULL || fmt == end_fmt) { #ifdef SHELL INTON; #endif - return (1); + return (fmt == NULL ? 1 : rval); } end = 0; } @@ -372,7 +374,7 @@ printf_doformat(char *fmt, int *rval) fputs(p, stdout); free(p); if (getout) - exit(*rval); + return (end_fmt); break; } case 'c': { -- cgit v1.1 From 2096c463802cf27feb8a38c7e34dc14d3a65fa50 Mon Sep 17 00:00:00 2001 From: jilles Date: Tue, 13 May 2014 21:24:55 +0000 Subject: printf: Fix missing arguments for %u/%o/%x/%X after r265592. If a numeric argument is missing, zero should be assumed, for signed as well as unsigned conversions. This fixes the 'zero' regression tests. r265592 erroneously reverted r244407. --- usr.bin/printf/printf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'usr.bin/printf') diff --git a/usr.bin/printf/printf.c b/usr.bin/printf/printf.c index 7a7e055..956e8a3 100644 --- a/usr.bin/printf/printf.c +++ b/usr.bin/printf/printf.c @@ -575,7 +575,7 @@ getnum(intmax_t *ip, uintmax_t *uip, int signedconv) int rval; if (!*gargv) { - *ip = 0; + *ip = *uip = 0; return (0); } if (**gargv == '"' || **gargv == '\'') { -- cgit v1.1 From 1818973ea93ed180c9365102a1bc98b1c6ac912b Mon Sep 17 00:00:00 2001 From: pfg Date: Mon, 26 May 2014 14:57:47 +0000 Subject: printf(1): warn about incomplete uses n$ Reviewed by: jilles Obtained from: Illumos MFC after: 2 weeks --- usr.bin/printf/printf.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'usr.bin/printf') diff --git a/usr.bin/printf/printf.c b/usr.bin/printf/printf.c index 956e8a3..5be9d05 100644 --- a/usr.bin/printf/printf.c +++ b/usr.bin/printf/printf.c @@ -244,11 +244,11 @@ printf_doformat(char *fmt, int *rval) /* save format argument */ fargv = gargv; } else { - fargv = NULL; + fargv = NULL; } /* skip to field width */ - while (strchr(skip1, *fmt) != NULL) { + while (*fmt && strchr(skip1, *fmt) != NULL) { *dptr++ = *fmt++; *dptr = 0; } @@ -259,12 +259,19 @@ printf_doformat(char *fmt, int *rval) l = strspn(fmt, digits); if ((l > 0) && (fmt[l] == '$')) { int idx = atoi(fmt); + if (fargv == NULL) { + warnx("incomplete use of n$"); + return (NULL); + } if (idx <= myargc) { gargv = &myargv[idx - 1]; } else { gargv = &myargv[myargc]; } fmt += l + 1; + } else if (fargv != NULL) { + warnx("incomplete use of n$"); + return (NULL); } if (getint(&fieldwidth)) @@ -296,12 +303,19 @@ printf_doformat(char *fmt, int *rval) l = strspn(fmt, digits); if ((l > 0) && (fmt[l] == '$')) { int idx = atoi(fmt); + if (fargv == NULL) { + warnx("incomplete use of n$"); + return (NULL); + } if (idx <= myargc) { gargv = &myargv[idx - 1]; } else { gargv = &myargv[myargc]; } fmt += l + 1; + } else if (fargv != NULL) { + warnx("incomplete use of n$"); + return (NULL); } if (getint(&precision)) -- cgit v1.1 From ac467aea1fc1dd31bcbc50ff9dd158fa91f6b8a9 Mon Sep 17 00:00:00 2001 From: pfg Date: Mon, 26 May 2014 15:08:39 +0000 Subject: printf(1): add tests for warn about incomplete uses n$ Submitted by: jilles MFC after: 2 weeks --- usr.bin/printf/tests/regress.missingpos1.out | 1 + usr.bin/printf/tests/regress.sh | 8 ++++++++ 2 files changed, 9 insertions(+) create mode 100644 usr.bin/printf/tests/regress.missingpos1.out (limited to 'usr.bin/printf') diff --git a/usr.bin/printf/tests/regress.missingpos1.out b/usr.bin/printf/tests/regress.missingpos1.out new file mode 100644 index 0000000..3b04f03 --- /dev/null +++ b/usr.bin/printf/tests/regress.missingpos1.out @@ -0,0 +1 @@ +printf: incomplete use of n$ diff --git a/usr.bin/printf/tests/regress.sh b/usr.bin/printf/tests/regress.sh index 84e183b..15b8284 100644 --- a/usr.bin/printf/tests/regress.sh +++ b/usr.bin/printf/tests/regress.sh @@ -19,5 +19,13 @@ REGRESSION_TEST('zero', `printf "%u%u\n" 15') REGRESSION_TEST('zero', `printf "%d%d\n" 15') REGRESSION_TEST('zero', `printf "%d%u\n" 15') REGRESSION_TEST('zero', `printf "%u%d\n" 15') +REGRESSION_TEST(`missingpos1', `printf "%1\$*s" 1 1 2>&1') +REGRESSION_TEST(`missingpos1', `printf "%*1\$s" 1 1 2>&1') +REGRESSION_TEST(`missingpos1', `printf "%1\$*.*s" 1 1 1 2>&1') +REGRESSION_TEST(`missingpos1', `printf "%*1\$.*s" 1 1 1 2>&1') +REGRESSION_TEST(`missingpos1', `printf "%*.*1\$s" 1 1 1 2>&1') +REGRESSION_TEST(`missingpos1', `printf "%1\$*2\$.*s" 1 1 1 2>&1') +REGRESSION_TEST(`missingpos1', `printf "%*1\$.*2\$s" 1 1 1 2>&1') +REGRESSION_TEST(`missingpos1', `printf "%1\$*.*2\$s" 1 1 1 2>&1') REGRESSION_END() -- cgit v1.1 From c2026d6193c04696f2a210ccd5fbfb8a03002fe5 Mon Sep 17 00:00:00 2001 From: pfg Date: Thu, 29 May 2014 19:43:43 +0000 Subject: Minor style knit. --- usr.bin/printf/printf.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'usr.bin/printf') diff --git a/usr.bin/printf/printf.c b/usr.bin/printf/printf.c index 5be9d05..452711e 100644 --- a/usr.bin/printf/printf.c +++ b/usr.bin/printf/printf.c @@ -456,8 +456,7 @@ mknum(char *str, char ch) len = strlen(str) + 2; if (len > copy_size) { newlen = ((len + 1023) >> 10) << 10; - if ((newcopy = realloc(copy, newlen)) == NULL) - { + if ((newcopy = realloc(copy, newlen)) == NULL) { warnx("%s", strerror(ENOMEM)); return (NULL); } -- cgit v1.1 From db1eda5727266ac3a90857dd7f8528f7f63c2a1a Mon Sep 17 00:00:00 2001 From: pfg Date: Thu, 29 May 2014 19:48:18 +0000 Subject: Update number of tests. Suggested by: jmmv --- usr.bin/printf/tests/regress.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'usr.bin/printf') diff --git a/usr.bin/printf/tests/regress.sh b/usr.bin/printf/tests/regress.sh index 15b8284..c9668a3 100644 --- a/usr.bin/printf/tests/regress.sh +++ b/usr.bin/printf/tests/regress.sh @@ -2,7 +2,7 @@ REGRESSION_START($1) -echo '1..15' +echo '1..23' REGRESSION_TEST(`b', `printf "abc%b%b" "def\n" "\cghi"') REGRESSION_TEST(`d', `printf "%d,%5d,%.5d,%0*d,%.*d\n" 123 123 123 5 123 5 123') -- cgit v1.1 From dee2131a39e2d4f12cc053fa1c5c2de12188b7e4 Mon Sep 17 00:00:00 2001 From: pfg Date: Sat, 31 May 2014 00:54:21 +0000 Subject: Fix m2 regression test. This is not really a good test as the behaviour for /c is unspecified. For the record, ksh93 returns: $ printf "abc\n\cdef" abc ef$ Discussed with: Garret D'Amore (Illumos) --- usr.bin/printf/tests/regress.m2.out | 1 + 1 file changed, 1 insertion(+) (limited to 'usr.bin/printf') diff --git a/usr.bin/printf/tests/regress.m2.out b/usr.bin/printf/tests/regress.m2.out index 8baef1b..cf14915 100644 --- a/usr.bin/printf/tests/regress.m2.out +++ b/usr.bin/printf/tests/regress.m2.out @@ -1 +1,2 @@ abc +cdef \ No newline at end of file -- cgit v1.1 From c9f472ff07cbeb0d5444489275e5a8afa3e2c704 Mon Sep 17 00:00:00 2001 From: jilles Date: Sun, 1 Jun 2014 13:33:22 +0000 Subject: printf: Install tests/regress.missingpos1.out, fixing tests. --- usr.bin/printf/tests/Makefile | 1 + 1 file changed, 1 insertion(+) (limited to 'usr.bin/printf') diff --git a/usr.bin/printf/tests/Makefile b/usr.bin/printf/tests/Makefile index da3f533..5e212ef 100644 --- a/usr.bin/printf/tests/Makefile +++ b/usr.bin/printf/tests/Makefile @@ -15,6 +15,7 @@ FILES+= regress.m2.out FILES+= regress.m3.out FILES+= regress.m4.out FILES+= regress.m5.out +FILES+= regress.missingpos1.out FILES+= regress.s.out FILES+= regress.sh FILES+= regress.zero.out -- cgit v1.1