[Libguestfs] [PATCH libnbd 1/4] copy: nbd-ops.c: Less convoluted error handling

Nir Soffer nirsof at gmail.com
Mon Mar 1 16:57:43 UTC 2021


Jumping back to error label works, but is harder to follow. Move the
error handlers to the end of the function.

Signed-off-by: Nir Soffer <nsoffer at redhat.com>
---
 copy/nbd-ops.c | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/copy/nbd-ops.c b/copy/nbd-ops.c
index 2af09b2..cb11e64 100644
--- a/copy/nbd-ops.c
+++ b/copy/nbd-ops.c
@@ -136,18 +136,19 @@ nbd_rw_create_subprocess (const char **argv, size_t argc, direction d)
 
   /* We have to copy the args so we can null-terminate them. */
   for (i = 0; i < argc; ++i) {
-    if (const_string_vector_append (&rwn->argv, argv[i]) == -1) {
-    memory_error:
-      perror ("realloc");
-      exit (EXIT_FAILURE);
-    }
+    if (const_string_vector_append (&rwn->argv, argv[i]) == -1)
+      goto error;
   }
   if (const_string_vector_append (&rwn->argv, NULL) == -1)
-    goto memory_error;
+    goto error;
 
   open_one_nbd_handle (rwn);
 
   return &rwn->rw;
+
+error:
+  perror ("realloc");
+  exit (EXIT_FAILURE);
 }
 
 static void
@@ -384,14 +385,18 @@ nbd_ops_get_polling_fd (struct rw *rw, uintptr_t index,
   nbd = rwn->handles.ptr[index];
 
   *fd = nbd_aio_get_fd (nbd);
-  if (*fd == -1) {
-  error:
-    fprintf (stderr, "%s: %s\n", rw->name, nbd_get_error ());
-    exit (EXIT_FAILURE);
-  }
+  if (*fd == -1)
+    goto error;
+
   *direction = nbd_aio_get_direction (nbd);
   if (*direction == -1)
     goto error;
+
+  return;
+
+error:
+  fprintf (stderr, "%s: %s\n", rw->name, nbd_get_error ());
+  exit (EXIT_FAILURE);
 }
 
 static void
-- 
2.26.2




More information about the Libguestfs mailing list