Do not leak write_fd in call_dump error path
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Fri, 14 Feb 2020 13:09:45 +0000 (14:09 +0100)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Fri, 14 Feb 2020 13:10:24 +0000 (14:10 +0100)
Change-Id: I1ac5ca3a3987c2334e013bd52f304aa5fa0f9f58

src/dumpsys/dumpsys.c

index 93d1cd1..6f4e4ac 100644 (file)
@@ -170,19 +170,19 @@ static void call_dump(const gchar *name, const gchar *application_name, GDBusCon
                return;
        }
 
+       g_unix_fd_list_append(fd_list, write_fd, &error);
+       if (error != NULL) {
+               dprintf(STDERR_FILENO, "g_unix_fd_list_append() error: %s\n", error->message);
+               goto out_close;
+       }
+
        pthread_t copy_thread;
        int fd_pair2[2];
        fd_pair2[0] = read_fd;
        fd_pair2[1] = out_fd;
        if (pthread_create(&copy_thread, NULL, copy_data, &fd_pair2[0]) != 0) {
                printf("pthread_create error: %m\n");
-               return;
-       }
-       g_unix_fd_list_append(fd_list, write_fd, &error);
-       close(write_fd);
-       if (error != NULL) {
-               dprintf(STDERR_FILENO, "g_unix_fd_list_append() error: %s\n", error->message);
-               return;
+               goto out_unref_fdlist;
        }
 
        g_dbus_message_set_unix_fd_list(method_call_message, fd_list);
@@ -203,13 +203,16 @@ static void call_dump(const gchar *name, const gchar *application_name, GDBusCon
                g_error_free(error);
        }
 
-       g_object_unref(fd_list);
        g_object_unref(method_call_message);
        g_object_unref(repl);
 
        if (pthread_join(copy_thread, NULL) != 0)
                printf("pthread_join error: %m\n");
 
+out_unref_fdlist:
+       g_object_unref(fd_list);
+out_close:
+       close(write_fd);
        close(read_fd);
 }