lib: fix snprintf() for UTF-16 strings
authorHeinrich Schuchardt <heinrich.schuchardt@canonical.com>
Sat, 29 Jan 2022 15:43:20 +0000 (16:43 +0100)
committerHeinrich Schuchardt <heinrich.schuchardt@canonical.com>
Sat, 5 Feb 2022 19:20:01 +0000 (20:20 +0100)
snprintf() must return the required buffer length.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
lib/vsprintf.c

index 69b2f6a..fe06aa2 100644 (file)
@@ -285,15 +285,25 @@ static __maybe_unused char *string16(char *buf, char *end, u16 *s,
        if (!(flags & LEFT))
                for (; len < field_width; --field_width)
                        ADDCH(buf, ' ');
-       for (i = 0; i < len && buf + utf16_utf8_strnlen(str, 1) <= end; ++i) {
+       if (buf < end)
+               *buf = 0;
+       for (i = 0; i < len; ++i) {
+               int slen = utf16_utf8_strnlen(str, 1);
                s32 s = utf16_get(&str);
 
                if (s < 0)
                        s = '?';
-               utf8_put(s, &buf);
+               if (buf + slen < end) {
+                       utf8_put(s, &buf);
+                       if (buf < end)
+                               *buf = 0;
+               } else {
+                       buf += slen;
+               }
        }
        for (; len < field_width; --field_width)
                ADDCH(buf, ' ');
+
        return buf;
 }