Skip to contentSkip to conversation

qemu-nbd: Fix a memleak in nbd_client_thread()

PATCH Superseded Alex Chen 2 participants 3 messages Dec 3, 2020 – Dec 8, 2020

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Alex Chen

author
#0 lore raw

PATCH v2 1/1 qemu-nbd: Fix a memleak in nbd_client_thread()

When the qio_channel_socket_connect_sync() fails
we should goto 'out_socket' label to free the 'sioc' instead of goto 'out' label.
In addition, there's a lot of redundant code in the successful branch and the error branch, optimize it.

---
 qemu-nbd.c | 38 +++++++++++++++-----------------------
 1 file changed, 15 insertions(+), 23 deletions(-)
qemu-nbd.c +15 -23
index a7075c5419..9583ee1af6 100644
@@ -265,8 +265,8 @@ static void *nbd_client_thread(void *arg)
     char *device = arg;
     NBDExportInfo info = { .request_sizes = false, .name = g_strdup("") };
     QIOChannelSocket *sioc;
-    int fd;
-    int ret;
+    int fd = -1;
+    int ret = EXIT_FAILURE;
     pthread_t show_parts_thread;
     Error *local_error = NULL;
 
@@ -278,26 +278,24 @@ static void *nbd_client_thread(void *arg)
         goto out;
     }
 
-    ret = nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc),
-                                NULL, NULL, NULL, &info, &local_error);
-    if (ret < 0) {
+    if (nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc),
+                              NULL, NULL, NULL, &info, &local_error) < 0) {
         if (local_error) {
             error_report_err(local_error);
         }
-        goto out_socket;
+        goto out;
     }
 
     fd = open(device, O_RDWR);
     if (fd < 0) {
         /* Linux-only, we can use %m in printf.  */
         error_report("Failed to open %s: %m", device);
-        goto out_socket;
+        goto out;
     }
 
-    ret = nbd_init(fd, sioc, &info, &local_error);
-    if (ret < 0) {
+    if (nbd_init(fd, sioc, &info, &local_error) < 0) {
         error_report_err(local_error);
-        goto out_fd;
+        goto out;
     }
 
     /* update partition table */
@@ -311,24 +309,18 @@ static void *nbd_client_thread(void *arg)
         dup2(STDOUT_FILENO, STDERR_FILENO);
     }
 
-    ret = nbd_client(fd);
-    if (ret) {
-        goto out_fd;
+    if (nbd_client(fd) == 0) {
+        ret = EXIT_SUCCESS;
     }
-    close(fd);
-    object_unref(OBJECT(sioc));
-    g_free(info.name);
-    kill(getpid(), SIGTERM);
-    return (void *) EXIT_SUCCESS;
 
-out_fd:
-    close(fd);
-out_socket:
+ out:
+    if (fd >= 0) {
+        close(fd);
+    }
     object_unref(OBJECT(sioc));
-out:
     g_free(info.name);
     kill(getpid(), SIGTERM);
-    return (void *) EXIT_FAILURE;
+    return (void *) (intptr_t) ret;
 }
 #endif /* HAVE_NBD_DEVICE */
 
Signature · 1 line
2.19.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 62 lines · Alex Chen

When the qio_channel_socket_connect_sync() fails
we should goto 'out_socket' label to free the 'sioc' instead of goto 'out' label.
In addition, there's a lot of redundant code in the successful branch and the error branch, optimize it.

---
  qemu-nbd.c | 38 +++++++++++++++-----------------------
  1 file changed, 15 insertions(+), 23 deletions(-)
qemu-nbd.c +8 -10
index a7075c5419..9583ee1af6 100644
@@ -265,8 +265,8 @@ static void *nbd_client_thread(void *arg)
      char *device = arg;
      NBDExportInfo info = { .request_sizes = false, .name = g_strdup("") };
      QIOChannelSocket *sioc;
-    int fd;
-    int ret;
+    int fd = -1;
+    int ret = EXIT_FAILURE;
      pthread_t show_parts_thread;
      Error *local_error = NULL;
 
@@ -278,26 +278,24 @@ static void *nbd_client_thread(void *arg)
          goto out;
      }
 
-    ret = nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc),
-                                NULL, NULL, NULL, &info, &local_error);
-    if (ret < 0) {
+    if (nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc),
+                              NULL, NULL, NULL, &info, &local_error) < 0) {
          if (local_error) {
              error_report_err(local_error);
          }
-        goto out_socket;
+        goto out;
      }
 
      fd = open(device, O_RDWR);
      if (fd < 0) {
          /* Linux-only, we can use %m in printf.  */
          error_report("Failed to open %s: %m", device);
-        goto out_socket;
+        goto out;
      }
 
-    ret = nbd_init(fd, sioc, &info, &local_error);
-    if (ret < 0) {
+    if (nbd_init(fd, sioc, &info, &local_error) < 0) {
          error_report_err(local_error);
-        goto out_fd;
+        goto out;
      }
 
      /* update partition table */

qemu-nbd.c static void *nbd_client_thread(void *arg) · line 313

Quoted 8 lines
qemu-nbd.c +2 -3
@@ -311,24 +309,18 @@ static void *nbd_client_thread(void *arg)
          dup2(STDOUT_FILENO, STDERR_FILENO);
      }
 
-    ret = nbd_client(fd);
-    if (ret) {
-        goto out_fd;
+    if (nbd_client(fd) == 0) {
+        ret = EXIT_SUCCESS;

It's not obvious that nbd_client() returns 0 on success, it calls ioctl(), which may return something positive in theory..

So, with s/==/>=/, or with just

if (nbd_client(fd) < 0) {
goto out;
}

ret = EXIT_SUCCESS;

(which is good common pattern I think)

:

Signature · 2 lines
Best regards,
Vladimir
author
#2 lore raw
Quoted 74 lines · Vladimir Sementsov-Ogievskiy
Quoted 57 lines · Alex Chen

When the qio_channel_socket_connect_sync() fails
we should goto 'out_socket' label to free the 'sioc' instead of goto 'out' label.
In addition, there's a lot of redundant code in the successful branch and the error branch, optimize it.

---
  qemu-nbd.c | 38 +++++++++++++++-----------------------
  1 file changed, 15 insertions(+), 23 deletions(-)
qemu-nbd.c +2 -2
index a7075c5419..9583ee1af6 100644
@@ -265,8 +265,8 @@ static void *nbd_client_thread(void *arg)
      char *device = arg;
      NBDExportInfo info = { .request_sizes = false, .name = g_strdup("") };
      QIOChannelSocket *sioc;
-    int fd;
-    int ret;
+    int fd = -1;
+    int ret = EXIT_FAILURE;
      pthread_t show_parts_thread;
      Error *local_error = NULL;
  @@ -278,26 +278,24 @@ static void *nbd_client_thread(void *arg)
          goto out;
      }

- ret = nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc),

qemu-nbd.c +6 -6
-                                NULL, NULL, NULL, &info, &local_error);
-    if (ret < 0) {
+    if (nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc),
+                              NULL, NULL, NULL, &info, &local_error) < 0) {
          if (local_error) {
              error_report_err(local_error);
          }
-        goto out_socket;
+        goto out;
      }
        fd = open(device, O_RDWR);
      if (fd < 0) {
          /* Linux-only, we can use %m in printf.  */
          error_report("Failed to open %s: %m", device);
-        goto out_socket;
+        goto out;
      }
  -    ret = nbd_init(fd, sioc, &info, &local_error);
-    if (ret < 0) {
+    if (nbd_init(fd, sioc, &info, &local_error) < 0) {
          error_report_err(local_error);
-        goto out_fd;
+        goto out;
      }
        /* update partition table */

qemu-nbd.c static void *nbd_client_thread(void *arg) · line 313

Quoted 7 lines
qemu-nbd.c +2 -2
@@ -311,24 +309,18 @@ static void *nbd_client_thread(void *arg)
          dup2(STDOUT_FILENO, STDERR_FILENO);
      }
  -    ret = nbd_client(fd);
-    if (ret) {
-        goto out_fd;
+    if (nbd_client(fd) == 0) {
+        ret = EXIT_SUCCESS;

It's not obvious that nbd_client() returns 0 on success, it calls ioctl(), which may return something positive in theory..

So, with s/==/>=/, or with just

if (nbd_client(fd) < 0) {
goto out;
}

Quoted 7 lines

ret = EXIT_SUCCESS;

(which is good common pattern I think)

:

Thanks for your review, I will fix it and send patch v3.

Thanks,
Alex