On Thu, 25 Aug 2016 13:56:24 -0700, Philip Guenther wrote:

> This:
> 
> if (blah) {
>     size_t len;
>     ...
> } else {
>     size_t len;
>     ...
> }
> 
> looks noisy to me, so I would lean towards your latter idea.

Yeah, I just liked len being scoped that way.  However, I see other
places we really want to use a size_t for length so I suppose we
can use it elsewhere too.

 - todd

Index: lib/libc/stdio/vfprintf.c
===================================================================
RCS file: /cvs/src/lib/libc/stdio/vfprintf.c,v
retrieving revision 1.75
diff -u -p -u -r1.75 vfprintf.c
--- lib/libc/stdio/vfprintf.c   17 Aug 2016 22:15:08 -0000      1.75
+++ lib/libc/stdio/vfprintf.c   25 Aug 2016 21:39:16 -0000
@@ -486,6 +486,8 @@ __vfprintf(FILE *fp, const char *fmt0, _
         * 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':

Reply via email to