From fffc361170ba79fc9defac866c0e75de9342cc02 Mon Sep 17 00:00:00 2001 From: guenther Date: Fri, 18 Apr 2014 11:35:51 +0000 Subject: [PATCH] Handle passing zero to a variable fieldwidth or precision. ok deraadt@ --- usr.bin/printf/printf.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/usr.bin/printf/printf.c b/usr.bin/printf/printf.c index ea536015850..60cee4a5144 100644 --- a/usr.bin/printf/printf.c +++ b/usr.bin/printf/printf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: printf.c,v 1.19 2013/11/20 20:46:47 deraadt Exp $ */ +/* $OpenBSD: printf.c,v 1.20 2014/04/18 11:35:51 guenther Exp $ */ /* * Copyright (c) 1989 The Regents of the University of California. @@ -59,12 +59,12 @@ static char **gargv; #define hextobin(c) ((c) >= 'A' && (c) <= 'F' ? c - 'A' + 10 : (c) >= 'a' && (c) <= 'f' ? c - 'a' + 10 : c - '0') #define PF(f, func) { \ - if (fieldwidth) \ - if (precision) \ + if (havefieldwidth) \ + if (haveprecision) \ (void)printf(f, fieldwidth, precision, func); \ else \ (void)printf(f, fieldwidth, func); \ - else if (precision) \ + else if (haveprecision) \ (void)printf(f, precision, func); \ else \ (void)printf(f, func); \ @@ -74,6 +74,7 @@ int main(int argc, char *argv[]) { char *fmt, *start; + int havefieldwidth, haveprecision; int fieldwidth, precision; char convch, nextch; char *format; @@ -128,18 +129,20 @@ main(int argc, char *argv[]) ; if (*fmt == '*') { ++fmt; + havefieldwidth = 1; fieldwidth = getint(); } else - fieldwidth = 0; + havefieldwidth = 0; /* skip to field precision */ for (; strchr(SKIP2, *fmt); ++fmt) ; - precision = 0; + haveprecision = 0; if (*fmt == '.') { ++fmt; if (*fmt == '*') { ++fmt; + haveprecision = 1; precision = getint(); } for (; strchr(SKIP2, *fmt); ++fmt) -- 2.20.1