diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2016-08-27 16:10:41 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2016-08-27 16:10:41 +0000 |
commit | edd31b1f0aad008556fec49f69b79f2c93d42f27 (patch) | |
tree | a4748e74b0c38bb22c90bba627b2cd58887a1e54 | |
parent | 36264f976658e8590fba75250ac4b63fde23cd1a (diff) |
When a precision is specified for a string format use strnlen()
to determine the length instead of doing it manually. OK schwarze@
-rw-r--r-- | lib/libc/stdio/vfprintf.c | 24 |
1 files changed, 7 insertions, 17 deletions
diff --git a/lib/libc/stdio/vfprintf.c b/lib/libc/stdio/vfprintf.c index 1b497515500..49fa61af607 100644 --- a/lib/libc/stdio/vfprintf.c +++ b/lib/libc/stdio/vfprintf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vfprintf.c,v 1.75 2016/08/17 22:15:08 tedu Exp $ */ +/* $OpenBSD: vfprintf.c,v 1.76 2016/08/27 16:10:40 millert Exp $ */ /*- * Copyright (c) 1990 The Regents of the University of California. * All rights reserved. @@ -486,6 +486,8 @@ __vfprintf(FILE *fp, const char *fmt0, __va_list ap) * Scan the format for conversions (`%' character). */ for (;;) { + size_t len; + cp = fmt; while ((n = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) { fmt += n; @@ -886,22 +888,10 @@ fp_common: cp = "(null)"; } - if (prec >= 0) { - /* - * can't use strlen; can only look for the - * NUL in the first `prec' characters, and - * strlen() will go further. - */ - char *p = memchr(cp, 0, prec); - - size = p ? (p - cp) : prec; - } else { - size_t len; - - if ((len = strlen(cp)) > INT_MAX) - goto overflow; - size = (int)len; - } + len = prec >= 0 ? strnlen(cp, prec) : strlen(cp); + if (len > INT_MAX) + goto overflow; + size = (int)len; sign = '\0'; break; case 'U': |