Skip to content

Commit

Permalink
safe_macros: Fix confusing safe_read() failure output
Browse files Browse the repository at this point in the history
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.

Reported-by: Andrea Cervesato <[email protected]>
Signed-off-by: Cyril Hrubis <[email protected]>
Reviewed-by: Petr Vorel <[email protected]>
  • Loading branch information
metan-ucw committed Jan 16, 2025
1 parent 4c68d72 commit 578ba63
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/safe_macros.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,18 @@ 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);
} else if (rval < 0) {
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;
Expand Down

0 comments on commit 578ba63

Please sign in to comment.