linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] selftests/nolibc: correctly report errors from printf() and friends
@ 2025-07-04 13:43 Thomas Weißschuh
  2025-07-04 13:43 ` [PATCH 1/2] selftests/nolibc: create /dev/full when running as PID 1 Thomas Weißschuh
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Thomas Weißschuh @ 2025-07-04 13:43 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, Shuah Khan,
	Paul E. McKenney
  Cc: linux-kselftest, linux-kernel, Thomas Weißschuh

When an error is encountered by printf() it needs to be reported.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
---
Thomas Weißschuh (2):
      selftests/nolibc: create /dev/full when running as PID 1
      selftests/nolibc: correctly report errors from printf() and friends

 tools/include/nolibc/stdio.h                 |  4 ++--
 tools/testing/selftests/nolibc/nolibc-test.c | 27 ++++++++++++++++++++++++++-
 2 files changed, 28 insertions(+), 3 deletions(-)
---
base-commit: 1536aa0fb1e09cb50f401ec4852c60f38173d751
change-id: 20250704-nolibc-printf-error-aa54f951c0a4

Best regards,
-- 
Thomas Weißschuh <thomas.weissschuh@linutronix.de>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] selftests/nolibc: create /dev/full when running as PID 1
  2025-07-04 13:43 [PATCH 0/2] selftests/nolibc: correctly report errors from printf() and friends Thomas Weißschuh
@ 2025-07-04 13:43 ` Thomas Weißschuh
  2025-07-04 13:43 ` [PATCH 2/2] selftests/nolibc: correctly report errors from printf() and friends Thomas Weißschuh
  2025-07-06  6:29 ` [PATCH 0/2] " Willy Tarreau
  2 siblings, 0 replies; 4+ messages in thread
From: Thomas Weißschuh @ 2025-07-04 13:43 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, Shuah Khan,
	Paul E. McKenney
  Cc: linux-kselftest, linux-kernel, Thomas Weißschuh

An upcoming testcase will use /dev/full.

Make sure it is always present.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
---
 tools/testing/selftests/nolibc/nolibc-test.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index b5bca1dcf36e95a576ca9ffba4f7c213978a3f35..929b966099b369aeca6d43002ebb0a160674dddc 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -1777,12 +1777,14 @@ int prepare(void)
 	if (stat("/dev/.", &stat_buf) == 0 || mkdir("/dev", 0755) == 0) {
 		if (stat("/dev/console", &stat_buf) != 0 ||
 		    stat("/dev/null", &stat_buf) != 0 ||
-		    stat("/dev/zero", &stat_buf) != 0) {
+		    stat("/dev/zero", &stat_buf) != 0 ||
+		    stat("/dev/full", &stat_buf) != 0) {
 			/* try devtmpfs first, otherwise fall back to manual creation */
 			if (mount("/dev", "/dev", "devtmpfs", 0, 0) != 0) {
 				mknod("/dev/console", 0600 | S_IFCHR, makedev(5, 1));
 				mknod("/dev/null",    0666 | S_IFCHR, makedev(1, 3));
 				mknod("/dev/zero",    0666 | S_IFCHR, makedev(1, 5));
+				mknod("/dev/full",    0666 | S_IFCHR, makedev(1, 7));
 			}
 		}
 	}

-- 
2.50.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] selftests/nolibc: correctly report errors from printf() and friends
  2025-07-04 13:43 [PATCH 0/2] selftests/nolibc: correctly report errors from printf() and friends Thomas Weißschuh
  2025-07-04 13:43 ` [PATCH 1/2] selftests/nolibc: create /dev/full when running as PID 1 Thomas Weißschuh
@ 2025-07-04 13:43 ` Thomas Weißschuh
  2025-07-06  6:29 ` [PATCH 0/2] " Willy Tarreau
  2 siblings, 0 replies; 4+ messages in thread
From: Thomas Weißschuh @ 2025-07-04 13:43 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, Shuah Khan,
	Paul E. McKenney
  Cc: linux-kselftest, linux-kernel, Thomas Weißschuh

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>
---
 tools/include/nolibc/stdio.h                 |  4 ++--
 tools/testing/selftests/nolibc/nolibc-test.c | 23 +++++++++++++++++++++++
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index c470d334ef3f410d0584e01045d380347d61ad95..7630234408c584a560b059f0a7e30d407d186f17 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -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;
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 929b966099b369aeca6d43002ebb0a160674dddc..2835f65bda30ffabde749f42a6d3dfecc9b555fd 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -1661,6 +1661,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;
@@ -1690,6 +1712,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 */

-- 
2.50.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 0/2] selftests/nolibc: correctly report errors from printf() and friends
  2025-07-04 13:43 [PATCH 0/2] selftests/nolibc: correctly report errors from printf() and friends Thomas Weißschuh
  2025-07-04 13:43 ` [PATCH 1/2] selftests/nolibc: create /dev/full when running as PID 1 Thomas Weißschuh
  2025-07-04 13:43 ` [PATCH 2/2] selftests/nolibc: correctly report errors from printf() and friends Thomas Weißschuh
@ 2025-07-06  6:29 ` Willy Tarreau
  2 siblings, 0 replies; 4+ messages in thread
From: Willy Tarreau @ 2025-07-06  6:29 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: Thomas Weißschuh, Shuah Khan, Paul E. McKenney,
	linux-kselftest, linux-kernel

On Fri, Jul 04, 2025 at 03:43:11PM +0200, Thomas Weißschuh wrote:
> When an error is encountered by printf() it needs to be reported.
> 
> Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>

Good catch!

Acked-by: Willy Tarreau <w@1wt.eu>

Willy

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-07-06  6:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-04 13:43 [PATCH 0/2] selftests/nolibc: correctly report errors from printf() and friends Thomas Weißschuh
2025-07-04 13:43 ` [PATCH 1/2] selftests/nolibc: create /dev/full when running as PID 1 Thomas Weißschuh
2025-07-04 13:43 ` [PATCH 2/2] selftests/nolibc: correctly report errors from printf() and friends Thomas Weißschuh
2025-07-06  6:29 ` [PATCH 0/2] " Willy Tarreau

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).