* [LTP] [PATCH] safe_macros: Fix confusing safe_read() failure output
@ 2025-01-09 15:03 Cyril Hrubis
2025-01-09 16:18 ` Petr Vorel
0 siblings, 1 reply; 5+ messages in thread
From: Cyril Hrubis @ 2025-01-09 15:03 UTC (permalink / raw)
To: ltp
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 <andrea.cervesato@suse.com>
Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
lib/safe_macros.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/lib/safe_macros.c b/lib/safe_macros.c
index 109268587..b224a5861 100644
--- a/lib/safe_macros.c
+++ b/lib/safe_macros.c
@@ -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;
--
2.45.2
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [LTP] [PATCH] safe_macros: Fix confusing safe_read() failure output
2025-01-09 15:03 [LTP] [PATCH] safe_macros: Fix confusing safe_read() failure output Cyril Hrubis
@ 2025-01-09 16:18 ` Petr Vorel
2025-01-16 11:15 ` Cyril Hrubis
0 siblings, 1 reply; 5+ messages in thread
From: Petr Vorel @ 2025-01-09 16:18 UTC (permalink / raw)
To: Cyril Hrubis; +Cc: ltp
Hi Cyril, Andrea
> 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/
> message on a short read() while developing tests.
Good catch!
Reviewed-by: Petr Vorel <pvorel@suse.cz>
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);
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [LTP] [PATCH] safe_macros: Fix confusing safe_read() failure output
2025-01-09 16:18 ` Petr Vorel
@ 2025-01-16 11:15 ` Cyril Hrubis
2025-01-16 11:19 ` Petr Vorel
0 siblings, 1 reply; 5+ messages in thread
From: Cyril Hrubis @ 2025-01-16 11:15 UTC (permalink / raw)
To: Petr Vorel; +Cc: ltp
Hi!
> > 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/
>
> > message on a short read() while developing tests.
>
> Good catch!
>
> Reviewed-by: Petr Vorel <pvorel@suse.cz>
Pushed, thanks.
> 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);
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:
diff --git a/lib/safe_macros.c b/lib/safe_macros.c
index b224a5861..3d3e7c693 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;
}
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [LTP] [PATCH] safe_macros: Fix confusing safe_read() failure output
2025-01-16 11:15 ` Cyril Hrubis
@ 2025-01-16 11:19 ` Petr Vorel
2025-01-16 12:01 ` Cyril Hrubis
0 siblings, 1 reply; 5+ messages in thread
From: Petr Vorel @ 2025-01-16 11:19 UTC (permalink / raw)
To: Cyril Hrubis; +Cc: ltp
Hi Cyril,
> Hi!
> > > 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/
> > > message on a short read() while developing tests.
> > Good catch!
> > Reviewed-by: Petr Vorel <pvorel@suse.cz>
> Pushed, thanks.
> > 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);
> 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:
> diff --git a/lib/safe_macros.c b/lib/safe_macros.c
> index b224a5861..3d3e7c693 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;
> }
Reviewed-by: Petr Vorel <pvorel@suse.cz>
Please merge it directly (ideally with link to this discussion on lore).
Kind regards,
Petr
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [LTP] [PATCH] safe_macros: Fix confusing safe_read() failure output
2025-01-16 11:19 ` Petr Vorel
@ 2025-01-16 12:01 ` Cyril Hrubis
0 siblings, 0 replies; 5+ messages in thread
From: Cyril Hrubis @ 2025-01-16 12:01 UTC (permalink / raw)
To: Petr Vorel; +Cc: ltp
Hi!
> Reviewed-by: Petr Vorel <pvorel@suse.cz>
>
> Please merge it directly (ideally with link to this discussion on lore).
Done, thanks.
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-01-16 12:01 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-09 15:03 [LTP] [PATCH] safe_macros: Fix confusing safe_read() failure output Cyril Hrubis
2025-01-09 16:18 ` Petr Vorel
2025-01-16 11:15 ` Cyril Hrubis
2025-01-16 11:19 ` Petr Vorel
2025-01-16 12:01 ` Cyril Hrubis
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox