On Sat, 27 Aug 2016 12:12:02 +0200, Ingo Schwarze wrote:

> However, in the case at hand, it really helps readability
> in a function (unavoidably) already longer than comfortable
> for reading it.

Now that we have a handy size_t scratch variable, we can use it to
store the return value of mbrtowc() instead of storing it in an
int.  Worth it or overkill?

 - todd

Index: lib/libc/stdio/vfprintf.c
===================================================================
RCS file: /cvs/src/lib/libc/stdio/vfprintf.c,v
retrieving revision 1.76
diff -u -p -u -r1.76 vfprintf.c
--- lib/libc/stdio/vfprintf.c   27 Aug 2016 16:10:40 -0000      1.76
+++ lib/libc/stdio/vfprintf.c   27 Aug 2016 16:26:04 -0000
@@ -489,17 +489,17 @@ __vfprintf(FILE *fp, const char *fmt0, _
                size_t len;
 
                cp = fmt;
-               while ((n = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) {
-                       fmt += n;
+               while ((len = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) != 0) {
+                       if (len == (size_t)-1 || len == (size_t)-2) {
+                               ret = -1;
+                               goto error;
+                       }
+                       fmt += len;
                        if (wc == '%') {
                                fmt--;
                                break;
                        }
                }
-               if (n < 0) {
-                       ret = -1;
-                       goto error;
-               }
                if (fmt != cp) {
                        ptrdiff_t m = fmt - cp;
                        if (m < 0 || m > INT_MAX - ret)
@@ -507,7 +507,7 @@ __vfprintf(FILE *fp, const char *fmt0, _
                        PRINT(cp, m);
                        ret += m;
                }
-               if (n == 0)
+               if (len == 0)
                        goto done;
                fmt++;          /* skip over '%' */
 
@@ -1217,17 +1217,19 @@ __find_arguments(const char *fmt0, va_li
         * Scan the format for conversions (`%' character).
         */
        for (;;) {
+               size_t len;
+
                cp = fmt;
-               while ((n = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) {
-                       fmt += n;
+               while ((len = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) != 0) {
+                       if (len == (size_t)-1 || len == (size_t)-2)
+                               return (-1);
+                       fmt += len;
                        if (wc == '%') {
                                fmt--;
                                break;
                        }
                }
-               if (n < 0)
-                       return (-1);
-               if (n == 0)
+               if (len == 0)
                        goto done;
                fmt++;          /* skip over '%' */
 

Reply via email to