Skip to contentSkip to conversation

safe_macros: Fix confusing safe_read() failure output

PATCH In review Cyril Hrubis 2 participants 5 messages Jan 9, 2025 – Jan 16, 2025 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 safe_macros: Fix confusing safe_read() failure output

In the case that we read() less bytes than expected in the strict mode we used the same tst_brk() as for the case when read() fails. However for short reads the errno is in an udefined state and we possibly end up with confusing TBROK message. Andrea reported EACESS ernno in the TBROK message on a short read() while developing tests.

---
 lib/safe_macros.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
lib/safe_macros.c +5 -1
index 109268587..b224a5861 100644
@@ -293,7 +293,7 @@ ssize_t safe_read(const char *file, const int lineno, void (*cleanup_fn) (void),
 
 	rval = read(fildes, buf, nbyte);
 
-	if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
+	if (rval == -1) {
 		tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
 			"read(%d,%p,%zu) failed, returned %zd", fildes, buf,
 			nbyte, rval);
@@ -301,6 +301,10 @@ ssize_t safe_read(const char *file, const int lineno, void (*cleanup_fn) (void),
 		tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
 			"Invalid read(%d,%p,%zu) return value %zd", fildes,
 			buf, nbyte, rval);
+	} else if (len_strict && (size_t)rval != nbyte) {
+		tst_brkm_(file, lineno, TBROK, cleanup_fn,
+			  "Short read(%d,%p,%zu) returned only %zd",
+			  fildes, buf, nbyte, rval);
 	}
 
 	return rval;
Signature · 5 lines
2.45.2


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 1/1
#1 lore raw

Hi Cyril, Andrea

Quoted 4 lines

In the case that we read() less bytes than expected in the strict mode we used the same tst_brk() as for the case when read() fails. However for short reads the errno is in an udefined state and we possibly end up with confusing TBROK message. Andrea reported EACESS ernno in the TBROK

nit: s/ernno/errno/

Quoted 1 line

message on a short read() while developing tests.

Good catch!

FYI safe_write() also has TERRNO in len_strict.

Kind regards,
Petr

+++ lib/safe_macros.c
@@ -554,7 +554,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);

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Hi!

Quoted 11 lines
Quoted 4 lines

In the case that we read() less bytes than expected in the strict mode we used the same tst_brk() as for the case when read() fails. However for short reads the errno is in an udefined state and we possibly end up with confusing TBROK message. Andrea reported EACESS ernno in the TBROK

nit: s/ernno/errno/

Quoted 1 line

message on a short read() while developing tests.

Good catch!

Pushed, thanks.

Quoted 7 lines

FYI safe_write() also has TERRNO in len_strict.

Kind regards,
Petr

+++ lib/safe_macros.c

Review ssize_t safe_write(const char *file, const int lineno, void (cleanup_fn) (void), · line 560

Quoted 8 lines
(quoted diff) +1 -1
@@ -554,7 +554,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);

I guess that we are missing check for invalid return value from write() as well and return in the rval == -1 branch, we probably need to add:

lib/safe_macros.c +8 -0
index b224a5861..3d3e7c693 100644
@@ -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;
                }
 
 
Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by
#3 lore raw

Hi Cyril,

Quoted 41 lines

Hi!

Quoted 11 lines
Quoted 4 lines

In the case that we read() less bytes than expected in the strict mode we used the same tst_brk() as for the case when read() fails. However for short reads the errno is in an udefined state and we possibly end up with confusing TBROK message. Andrea reported EACESS ernno in the TBROK

nit: s/ernno/errno/

Quoted 1 line

message on a short read() while developing tests.

Good catch!

Pushed, thanks.

Quoted 7 lines

FYI safe_write() also has TERRNO in len_strict.

Kind regards,
Petr

+++ lib/safe_macros.c

Review ssize_t safe_write(const char *file, const int lineno, void (cleanup_fn) (void), · line 560

Quoted 8 lines
(quoted diff) +1 -1
@@ -554,7 +554,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);

I guess that we are missing check for invalid return value from write() as well and return in the rval == -1 branch, we probably need to add:

lib/safe_macros.c +0 -0
index b224a5861..3d3e7c693 100644
@@ -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",

lib/safe_macros.c ssize_t safe_write(const char *file, const int lineno, void (cleanup_fn) (void), · line 562

Quoted 10 lines
lib/safe_macros.c +8 -0
                                fildes, buf, nbyte);
+                       return rval;
+               }
+
+               if (rval < 0) {
+                       tst_brkm_(file, lineno, TBROK, cleanup_fn,
+                                 "invalid write() return value %zi",
+                                 rval);
+                       return rval;
                }

Please merge it directly (ideally with link to this discussion on lore).

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp