Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

selftests/nolibc: correctly report errors from printf() and friends

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>

authored by

Thomas Weißschuh and committed by
Thomas Weißschuh
4a401290 2b1ed5f7

+25 -2
+2 -2
tools/include/nolibc/stdio.h
··· 358 358 n -= w; 359 359 while (width-- > w) { 360 360 if (cb(state, " ", 1) != 0) 361 - break; 361 + return -1; 362 362 written += 1; 363 363 } 364 364 if (cb(state, outstr, w) != 0) 365 - break; 365 + return -1; 366 366 } 367 367 368 368 written += len;
+23
tools/testing/selftests/nolibc/nolibc-test.c
··· 1662 1662 return 0; 1663 1663 } 1664 1664 1665 + static int test_printf_error(void) 1666 + { 1667 + int fd, ret, saved_errno; 1668 + 1669 + fd = open("/dev/full", O_RDWR); 1670 + if (fd == -1) 1671 + return 1; 1672 + 1673 + errno = 0; 1674 + ret = dprintf(fd, "foo"); 1675 + saved_errno = errno; 1676 + close(fd); 1677 + 1678 + if (ret != -1) 1679 + return 2; 1680 + 1681 + if (saved_errno != ENOSPC) 1682 + return 3; 1683 + 1684 + return 0; 1685 + } 1686 + 1665 1687 static int run_printf(int min, int max) 1666 1688 { 1667 1689 int test; ··· 1713 1691 CASE_TEST(width_trunc); EXPECT_VFPRINTF(25, " ", "%25d", 1); break; 1714 1692 CASE_TEST(scanf); EXPECT_ZR(1, test_scanf()); break; 1715 1693 CASE_TEST(strerror); EXPECT_ZR(1, test_strerror()); break; 1694 + CASE_TEST(printf_error); EXPECT_ZR(1, test_printf_error()); break; 1716 1695 case __LINE__: 1717 1696 return ret; /* must be last */ 1718 1697 /* note: do not set any defaults so as to permit holes above */