[PATCH RISU] risu: Handle short reads when replaying a trace from stdin

Philippe Mathieu-Daudé posted 1 patch 3 days, 22 hours ago
There is a newer version of this series
risu.c | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
[PATCH RISU] risu: Handle short reads when replaying a trace from stdin
Posted by Philippe Mathieu-Daudé 3 days, 22 hours ago
With "-t -" the trace is read from a pipe, where read() may return
fewer bytes than requested, which was reported as an I/O error.
Loop until the whole record is read, retrying on EINTR.

Without zlib this is how a compressed trace has to be replayed:

  $ gunzip -c trace.gz | risu -t - image.bin

Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
---
 risu.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/risu.c b/risu.c
index c28b4a5..ba307f4 100644
--- a/risu.c
+++ b/risu.c
@@ -68,7 +68,21 @@ static RisuResult read_buffer(void *ptr, size_t bytes)
 #ifdef HAVE_ZLIB
     if (comm_fd == STDIN_FILENO) {
 #endif
-        res = read(comm_fd, ptr, bytes);
+        /* A pipe can return short reads: loop until we have it all. */
+        char *p = ptr;
+
+        res = 0;
+        while (res < bytes) {
+            ssize_t r = read(comm_fd, p + res, bytes - res);
+
+            if (r < 0 && errno == EINTR) {
+                continue;
+            }
+            if (r <= 0) {
+                break;
+            }
+            res += r;
+        }
 #ifdef HAVE_ZLIB
     } else {
         res = gzread(gz_trace_file, ptr, bytes);
-- 
2.55.0


Re: [PATCH RISU] risu: Handle short reads when replaying a trace from stdin
Posted by Richard Henderson 3 days, 21 hours ago
On 12/2/26 13:51, Philippe Mathieu-Daudé wrote:
> With "-t -" the trace is read from a pipe, where read() may return
> fewer bytes than requested, which was reported as an I/O error.
> Loop until the whole record is read, retrying on EINTR.
> 
> Without zlib this is how a compressed trace has to be replayed:
> 
>    $ gunzip -c trace.gz | risu -t - image.bin
> 
> Signed-off-by: Philippe Mathieu-Daudé<philmd@oss.qualcomm.com>
> ---
>   risu.c | 16 +++++++++++++++-
>   1 file changed, 15 insertions(+), 1 deletion(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

That said, why wouldn't you have native libz.a?

r~