]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
selftests/nolibc: correctly report errors from printf() and friends
authorThomas Weißschuh <thomas.weissschuh@linutronix.de>
Fri, 4 Jul 2025 13:43:13 +0000 (15:43 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 15 Aug 2025 14:38:31 +0000 (16:38 +0200)
[ Upstream commit 4a40129087a4c32135bb1177a57bbbe6ee646f1a ]

When an error is encountered by printf() it needs to be reported.
errno() is already set by the callback.

sprintf() is different, but that keeps working and is already tested.

Also add a new test.

Fixes: 7e4346f4a3a6 ("tools/nolibc/stdio: add a minimal [vf]printf() implementation")
Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://lore.kernel.org/r/20250704-nolibc-printf-error-v1-2-74b7a092433b@linutronix.de
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Signed-off-by: Sasha Levin <sashal@kernel.org>
tools/include/nolibc/stdio.h
tools/testing/selftests/nolibc/nolibc-test.c

index c470d334ef3f410d0584e01045d380347d61ad95..7630234408c584a560b059f0a7e30d407d186f17 100644 (file)
@@ -358,11 +358,11 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char
                                n -= w;
                                while (width-- > w) {
                                        if (cb(state, " ", 1) != 0)
-                                               break;
+                                               return -1;
                                        written += 1;
                                }
                                if (cb(state, outstr, w) != 0)
-                                       break;
+                                       return -1;
                        }
 
                        written += len;
index dbe13000fb1ac153e9a89f627492daeb584a05d4..b5c04c137249567694fcf25604125d23cce32120 100644 (file)
@@ -1646,6 +1646,28 @@ int test_strerror(void)
        return 0;
 }
 
+static int test_printf_error(void)
+{
+       int fd, ret, saved_errno;
+
+       fd = open("/dev/full", O_RDWR);
+       if (fd == -1)
+               return 1;
+
+       errno = 0;
+       ret = dprintf(fd, "foo");
+       saved_errno = errno;
+       close(fd);
+
+       if (ret != -1)
+               return 2;
+
+       if (saved_errno != ENOSPC)
+               return 3;
+
+       return 0;
+}
+
 static int run_printf(int min, int max)
 {
        int test;
@@ -1675,6 +1697,7 @@ static int run_printf(int min, int max)
                CASE_TEST(width_trunc);  EXPECT_VFPRINTF(25, "                    ", "%25d", 1); break;
                CASE_TEST(scanf);        EXPECT_ZR(1, test_scanf()); break;
                CASE_TEST(strerror);     EXPECT_ZR(1, test_strerror()); break;
+               CASE_TEST(printf_error); EXPECT_ZR(1, test_printf_error()); break;
                case __LINE__:
                        return ret; /* must be last */
                /* note: do not set any defaults so as to permit holes above */