From 869444a4196d84c0021ddd0c44c5434b02d3091b Mon Sep 17 00:00:00 2001 From: Cyril Hrubis Date: Thu, 16 Jan 2025 12:51:50 +0100 Subject: [PATCH] safe_macros: Fix safe_write() - Add return to the if (rval == -1) branch If we are in a cleanup the tst_brkm_() will return back and we will continue and print more tst_brkm_() messages particulary the "short write()" in the case of SAFE_WRITE_ALL - Add check for invalid return value (rval < 0) This will not happen unless there is a bug in libc but we should check for it anyways. - Remove TERRNO from the short write tst_brkm_() Since in a case of a short write the errno is not defined and we will print whatever was left there. Signed-off-by: Cyril Hrubis Reviewed-by: Petr Vorel Link: https://lore.kernel.org/ltp/20250109150336.25235-1-chrubis@suse.cz/ --- lib/safe_macros.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/safe_macros.c b/lib/safe_macros.c index b224a586147..6946cc5bcb9 100644 --- a/lib/safe_macros.c +++ b/lib/safe_macros.c @@ -551,6 +551,14 @@ ssize_t safe_write(const char *file, const int lineno, void (cleanup_fn) (void), tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn, "write(%d,%p,%zu) failed", fildes, buf, nbyte); + return rval; + } + + if (rval < 0) { + tst_brkm_(file, lineno, TBROK, cleanup_fn, + "invalid write() return value %zi", + rval); + return rval; } if (len_strict == SAFE_WRITE_ANY) @@ -558,7 +566,7 @@ ssize_t safe_write(const char *file, const int lineno, void (cleanup_fn) (void), if (len_strict == SAFE_WRITE_ALL) { if ((size_t)rval != nbyte) - tst_brkm_(file, lineno, TBROK | TERRNO, + tst_brkm_(file, lineno, TBROK, cleanup_fn, "short write(%d,%p,%zu) " "return value %zd", fildes, buf, nbyte, rval);