[Libguestfs] [PATCH v2] Daemon: fix handling of errors from xread and xwrite.

Richard W.M. Jones rjones at redhat.com
Thu Sep 17 14:50:01 UTC 2009


On Thu, Sep 17, 2009 at 03:39:45PM +0100, Daniel P. Berrange wrote:
> On Thu, Sep 17, 2009 at 03:33:24PM +0100, Richard W.M. Jones wrote:
> > 
> > This caused me to waste about 3 hours looking for a non-existent "data
> > corrupter" bug, which turned out to be the result of not checking the
> > return value of xread ...
> 
> You might want to add 
> 
>  __attribute__((__warn_unused_result__))
> 
> to the header file decl of xread/xwrite so that the compiler will
> warn if any other code areas forget to check the return error code

Good idea.  Updated patch attached.

Rich.

-- 
Richard Jones, Emerging Technologies, Red Hat  http://et.redhat.com/~rjones
virt-df lists disk usage of guests without needing to install any
software inside the virtual machine.  Supports Linux and Windows.
http://et.redhat.com/~rjones/virt-df/
-------------- next part --------------
>From af93e24c91d5448b34a7dcb0355b904516d531fc Mon Sep 17 00:00:00 2001
From: Richard Jones <rjones at trick.home.annexia.org>
Date: Thu, 17 Sep 2009 15:28:41 +0100
Subject: [PATCH 1/3] Daemon: fix handling of errors from xread and xwrite.

If xread or xwrite returns -1, that indicates an error and we
should exit.  Note that xread/xwrite has already printed the
error message.
---
 daemon/daemon.h   |    6 ++++--
 daemon/guestfsd.c |    3 ++-
 daemon/proto.c    |   18 +++++++++++-------
 3 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/daemon/daemon.h b/daemon/daemon.h
index 83c9672..86c6876 100644
--- a/daemon/daemon.h
+++ b/daemon/daemon.h
@@ -36,8 +36,10 @@ extern int sysroot_len;
 
 extern char *sysroot_path (const char *path);
 
-extern int xwrite (int sock, const void *buf, size_t len);
-extern int xread (int sock, void *buf, size_t len);
+extern int xwrite (int sock, const void *buf, size_t len)
+  __attribute__((__warn_unused_result__));
+extern int xread (int sock, void *buf, size_t len)
+  __attribute__((__warn_unused_result__));
 
 extern int add_string (char ***argv, int *size, int *alloc, const char *str);
 extern int count_strings (char *const *argv);
diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c
index ebaf960..bfd8139 100644
--- a/daemon/guestfsd.c
+++ b/daemon/guestfsd.c
@@ -197,7 +197,8 @@ main (int argc, char *argv[])
     exit (1);
   }
 
-  (void) xwrite (sock, buf, xdr_getpos (&xdr));
+  if (xwrite (sock, buf, xdr_getpos (&xdr)) == -1)
+    exit (1);
 
   xdr_destroy (&xdr);
 
diff --git a/daemon/proto.c b/daemon/proto.c
index 817a995..037a9e0 100644
--- a/daemon/proto.c
+++ b/daemon/proto.c
@@ -62,7 +62,9 @@ main_loop (int _sock)
 #endif
 
     /* Read the length word. */
-    xread (sock, lenbuf, 4);
+    if (xread (sock, lenbuf, 4) == -1)
+      exit (1);
+
     xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
     xdr_uint32_t (&xdr, &len);
     xdr_destroy (&xdr);
@@ -79,7 +81,8 @@ main_loop (int _sock)
       continue;
     }
 
-    xread (sock, buf, len);
+    if (xread (sock, buf, len) == -1)
+      exit (1);
 
 #if 0
     if (verbose) {
@@ -307,7 +310,9 @@ receive_file (receive_cb cb, void *opaque)
 
   for (;;) {
     /* Read the length word. */
-    xread (sock, lenbuf, 4);
+    if (xread (sock, lenbuf, 4) == -1)
+      exit (1);
+
     xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
     xdr_uint32_t (&xdr, &len);
     xdr_destroy (&xdr);
@@ -327,7 +332,8 @@ receive_file (receive_cb cb, void *opaque)
       return -1;
     }
 
-    xread (sock, buf, len);
+    if (xread (sock, buf, len) == -1)
+      exit (1);
 
     xdrmem_create (&xdr, buf, len, XDR_DECODE);
     memset (&chunk, 0, sizeof chunk);
@@ -444,10 +450,8 @@ check_for_library_cancellation (void)
 
   /* Read the message from the daemon. */
   r = xread (sock, buf, sizeof buf);
-  if (r == -1) {
-    perror ("read");
+  if (r == -1)
     return 0;
-  }
 
   xdrmem_create (&xdr, buf, sizeof buf, XDR_DECODE);
   xdr_uint32_t (&xdr, &flag);
-- 
1.6.2.5



More information about the Libguestfs mailing list