Skip to content

Commit

Permalink
safe_macros: Fix safe_write()
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
Reviewed-by: Petr Vorel <[email protected]>
Link: https://lore.kernel.org/ltp/[email protected]/
  • Loading branch information
metan-ucw committed Jan 16, 2025
1 parent 578ba63 commit 869444a
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/safe_macros.c
Original file line number Diff line number Diff line change
Expand Up @@ -551,14 +551,22 @@ 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)
return rval;

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);
Expand Down

0 comments on commit 869444a

Please sign in to comment.