Skip to content

Commit 4a40129

Browse files
committed
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: 7e4346f ("tools/nolibc/stdio: add a minimal [vf]printf() implementation") Signed-off-by: Thomas Weißschuh <[email protected]> Acked-by: Willy Tarreau <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Thomas Weißschuh <[email protected]>
1 parent 2b1ed5f commit 4a40129

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

tools/include/nolibc/stdio.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,11 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char
358358
n -= w;
359359
while (width-- > w) {
360360
if (cb(state, " ", 1) != 0)
361-
break;
361+
return -1;
362362
written += 1;
363363
}
364364
if (cb(state, outstr, w) != 0)
365-
break;
365+
return -1;
366366
}
367367

368368
written += len;

tools/testing/selftests/nolibc/nolibc-test.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,28 @@ int test_strerror(void)
16621662
return 0;
16631663
}
16641664

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+
16651687
static int run_printf(int min, int max)
16661688
{
16671689
int test;
@@ -1691,6 +1713,7 @@ static int run_printf(int min, int max)
16911713
CASE_TEST(width_trunc); EXPECT_VFPRINTF(25, " ", "%25d", 1); break;
16921714
CASE_TEST(scanf); EXPECT_ZR(1, test_scanf()); break;
16931715
CASE_TEST(strerror); EXPECT_ZR(1, test_strerror()); break;
1716+
CASE_TEST(printf_error); EXPECT_ZR(1, test_printf_error()); break;
16941717
case __LINE__:
16951718
return ret; /* must be last */
16961719
/* note: do not set any defaults so as to permit holes above */

0 commit comments

Comments
 (0)