[Libguestfs] [libnbd PATCH 1/4] examples: aio-connect-read.c: Fix error handling

Eric Blake eblake at redhat.com
Tue Feb 1 19:44:47 UTC 2022


This example failed to check the *error parameter to the read
completion callback.  Although the buffers in this example start life
zero-initialized due to living in .bss (so there is no leak of heap
contents), later reads could repeat the decoding of a buffer returned
from an earlier read rather than reporting the failure.

Fixed by reporting the error and exiting early.

Here is an example failure fixed by this patch:

$ ./run nbdkit -U - eval get_size='echo 64k' pread='echo EIO >&2; exit 1' \
  --run 'examples/aio-connect-read $unixsocket'
nbdkit: eval[1]: error: /tmp/nbdkit5kfDY2/pread:
failed to read: Input/output error
nbdkit: eval[1]: error: /tmp/nbdkit5kfDY2/pread:
nbdkit: eval[1]: error: write error reply: Broken pipe
$ echo $?
1

which previously dumped 64k of all zeroes before exiting with status 0.
---
 examples/aio-connect-read.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/examples/aio-connect-read.c b/examples/aio-connect-read.c
index 799525ad..26ac3f99 100644
--- a/examples/aio-connect-read.c
+++ b/examples/aio-connect-read.c
@@ -17,6 +17,7 @@
 #include <stdlib.h>
 #include <stdint.h>
 #include <inttypes.h>
+#include <errno.h>
 #include <assert.h>

 #include <libnbd.h>
@@ -35,6 +36,12 @@ hexdump (void *user_data, int *error)
   struct data *data = user_data;
   FILE *pp;

+  if (*error) {
+    errno = *error;
+    perror ("failed to read");
+    exit (EXIT_FAILURE);
+  }
+
   printf ("sector at offset 0x%" PRIx64 ":\n",
           data->offset);
   pp = popen ("hexdump -C", "w");
-- 
2.34.1




More information about the Libguestfs mailing list