From be13ccc67f2e566409afa30095175d55999921ee Mon Sep 17 00:00:00 2001 From: Fabian Keil Date: Wed, 3 Jun 2020 19:07:04 +0200 Subject: [PATCH 237/325] Revert "printf: Let printf_doformat() use malloc() to work around FreeBSD bug 246630" This reverts commit 476964e0034f0e69e9f7b99f81de7b8a2c30fba1. Obtained from: ElectroBSD --- usr.bin/printf/printf.c | 31 +++++-------------------------- 1 file changed, 5 insertions(+), 26 deletions(-) diff --git a/usr.bin/printf/printf.c b/usr.bin/printf/printf.c index 53b9c5607e06..94667f381a60 100644 --- a/usr.bin/printf/printf.c +++ b/usr.bin/printf/printf.c @@ -214,15 +214,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; - if ((start = malloc(strlen(fmt) + 1)) == NULL) { - warnx("%s", strerror(ENOMEM)); - return (NULL); - } dptr = start; *dptr++ = '%'; *dptr = 0; @@ -262,7 +258,6 @@ printf_doformat(char *fmt, int *rval) int idx = atoi(fmt); if (fargv == NULL) { warnx("incomplete use of n$"); - free(start); return (NULL); } if (idx <= myargc) { @@ -273,14 +268,11 @@ printf_doformat(char *fmt, int *rval) fmt += l + 1; } else if (fargv != NULL) { warnx("incomplete use of n$"); - free(start); return (NULL); } - if (getint(&fieldwidth)) { - free(start); + if (getint(&fieldwidth)) return (NULL); - } if (gargv > maxargv) maxargv = gargv; havewidth = 1; @@ -310,7 +302,6 @@ printf_doformat(char *fmt, int *rval) int idx = atoi(fmt); if (fargv == NULL) { warnx("incomplete use of n$"); - free(start); return (NULL); } if (idx <= myargc) { @@ -321,14 +312,11 @@ printf_doformat(char *fmt, int *rval) fmt += l + 1; } else if (fargv != NULL) { warnx("incomplete use of n$"); - free(start); return (NULL); } - if (getint(&precision)) { - free(start); + if (getint(&precision)) return (NULL); - } if (gargv > maxargv) maxargv = gargv; haveprec = 1; @@ -347,7 +335,6 @@ printf_doformat(char *fmt, int *rval) haveprec = 0; if (!*fmt) { warnx("missing format character"); - free(start); return (NULL); } *dptr++ = *fmt; @@ -368,7 +355,6 @@ printf_doformat(char *fmt, int *rval) fmt++; if (!strchr("aAeEfFgG", *fmt)) { warnx("bad modifier L for %%%c", *fmt); - free(start); return (NULL); } } else { @@ -394,7 +380,6 @@ printf_doformat(char *fmt, int *rval) start[strlen(start) - 1] = 's'; if ((p = strdup(getstr())) == NULL) { warnx("%s", strerror(ENOMEM)); - free(start); return (NULL); } getout = escape(p, 0, &len); @@ -402,10 +387,8 @@ printf_doformat(char *fmt, int *rval) /* Restore format for next loop. */ free(p); - if (getout) { - free(start); + if (getout) return (end_fmt); - } break; } case 'c': { @@ -430,10 +413,8 @@ printf_doformat(char *fmt, int *rval) int signedconv; signedconv = (convch == 'd' || convch == 'i'); - if ((f = mknum(start, convch)) == NULL) { - free(start); + if ((f = mknum(start, convch)) == NULL) return (NULL); - } if (getnum(&val, &uval, signedconv)) *rval = 1; if (signedconv) @@ -458,11 +439,9 @@ printf_doformat(char *fmt, int *rval) } default: warnx("illegal format character %c", convch); - free(start); return (NULL); } *fmt = nextch; - free(start); /* return the gargv to the next element */ return (fmt); } -- 2.32.0