]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests/nolibc: check vsnprintf() output buffer before the length
authorDavid Laight <david.laight.linux@gmail.com>
Mon, 2 Mar 2026 10:17:56 +0000 (10:17 +0000)
committerThomas Weißschuh <linux@weissschuh.net>
Fri, 20 Mar 2026 16:46:02 +0000 (17:46 +0100)
Check the string matches before checking the returned length.
Only print the string once when it matches.

Makes it a lot easier to diagnose any incorrect output.

Signed-off-by: David Laight <david.laight.linux@gmail.com>
Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://patch.msgid.link/20260302101815.3043-5-david.laight.linux@gmail.com
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
tools/testing/selftests/nolibc/nolibc-test.c

index 7588212df124852998b82962993975ca399bd3cc..bfe793caa9637bfeb0c076f63c3479ce91cd90a9 100644 (file)
@@ -1676,25 +1676,27 @@ static int expect_vfprintf(int llen, int c, const char *expected, const char *fm
        char buf[100];
        va_list args;
        ssize_t w;
-       int ret;
-
 
        va_start(args, fmt);
        /* Only allow writing 21 bytes, to test truncation */
        w = vsnprintf(buf, 21, fmt, args);
        va_end(args);
 
+       llen += printf(" \"%s\"", buf);
+       if (strncmp(expected, buf, c)) {
+               llen += printf(" should be \"%s\"", expected);
+               result(llen, FAIL);
+               return 1;
+       }
+
        if (w != c) {
                llen += printf(" written(%d) != %d", (int)w, c);
                result(llen, FAIL);
                return 1;
        }
 
-       llen += printf(" \"%s\" = \"%s\"", expected, buf);
-       ret = strncmp(expected, buf, c) != 0;
-
-       result(llen, ret ? FAIL : OK);
-       return ret;
+       result(llen, OK);
+       return 0;
 }
 
 static int test_scanf(void)