]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme: use cleanup helper for nvme_root_t objects
authorDaniel Wagner <dwagner@suse.de>
Wed, 27 Mar 2024 16:08:10 +0000 (17:08 +0100)
committerDaniel Wagner <wagi@monom.org>
Fri, 10 May 2024 07:21:39 +0000 (09:21 +0200)
Use a cleanup helper for the nvme_root_t objects which simplifies the
error paths and ensures we are always removing all the allocated
resources.

Signed-off-by: Daniel Wagner <dwagner@suse.de>
nvme.c
util/cleanup.h

diff --git a/nvme.c b/nvme.c
index 4a65c4b7c71825c818c2c88d05d34999c29bf16c..968ced583a443ca53d38cf1c33e294f7a8d1957c 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -3269,7 +3269,7 @@ static bool nvme_match_device_filter(nvme_subsystem_t s,
 static int list_subsys(int argc, char **argv, struct command *cmd,
                struct plugin *plugin)
 {
-       nvme_root_t r = NULL;
+       _cleanup_nvme_root_ nvme_root_t r = NULL;
        enum nvme_print_flags flags;
        const char *desc = "Retrieve information for subsystems";
        nvme_scan_filter_t filter = NULL;
@@ -3281,7 +3281,7 @@ static int list_subsys(int argc, char **argv, struct command *cmd,
 
        err = argconfig_parse(argc, argv, desc, opts);
        if (err < 0)
-               goto ret;
+               return err;
 
        devname = NULL;
        if (optind < argc)
@@ -3303,8 +3303,7 @@ static int list_subsys(int argc, char **argv, struct command *cmd,
                        nvme_show_error("Failed to scan nvme subsystem for %s", devname);
                else
                        nvme_show_error("Failed to scan nvme subsystem");
-               err = -errno;
-               goto ret;
+               return -errno;
        }
 
        if (devname) {
@@ -3312,8 +3311,7 @@ static int list_subsys(int argc, char **argv, struct command *cmd,
 
                if (sscanf(devname, "nvme%dn%d", &subsys_num, &nsid) != 2) {
                        nvme_show_error("Invalid device name %s", devname);
-                       err = -EINVAL;
-                       goto ret;
+                       return -EINVAL;
                }
                filter = nvme_match_device_filter;
        }
@@ -3322,22 +3320,19 @@ static int list_subsys(int argc, char **argv, struct command *cmd,
        if (err) {
                if (errno != ENOENT)
                        nvme_show_error("Failed to scan topology: %s", nvme_strerror(errno));
-               goto ret;
+               return -errno;
        }
 
        nvme_show_subsystem_list(r, nsid != NVME_NSID_ALL, flags);
 
-ret:
-       if (r)
-               nvme_free_tree(r);
-       return err;
+       return 0;
 }
 
 static int list(int argc, char **argv, struct command *cmd, struct plugin *plugin)
 {
        const char *desc = "Retrieve basic information for all NVMe namespaces";
        enum nvme_print_flags flags;
-       nvme_root_t r;
+       _cleanup_nvme_root_ nvme_root_t r = NULL;
        int err = 0;
 
        NVME_ARGS(opts);
@@ -3364,12 +3359,10 @@ static int list(int argc, char **argv, struct command *cmd, struct plugin *plugi
        if (err < 0) {
                if (errno != ENOENT)
                        nvme_show_error("Failed to scan topology: %s", nvme_strerror(errno));
-               nvme_free_tree(r);
                return err;
        }
 
        nvme_show_list_items(r, flags);
-       nvme_free_tree(r);
 
        return err;
 }
@@ -9508,7 +9501,7 @@ static int show_topology_cmd(int argc, char **argv, struct command *command, str
        const char *desc = "Show the topology\n";
        const char *ranking = "Ranking order: namespace|ctrl";
        enum nvme_print_flags flags;
-       nvme_root_t r;
+       _cleanup_nvme_root_ nvme_root_t r = NULL;
        enum nvme_cli_topo_ranking rank;
        int err;
 
@@ -9560,7 +9553,6 @@ static int show_topology_cmd(int argc, char **argv, struct command *command, str
        }
 
        nvme_show_topology(r, rank, flags);
-       nvme_free_tree(r);
 
        return err;
 }
index ee9b120606f5ec1a11e4e19cc628425ddc768ae8..c1e0394dc9e7ca772c3b631535b3f7e6919ff538 100644 (file)
@@ -5,6 +5,8 @@
 #include <unistd.h>
 #include <stdlib.h>
 
+#include <libnvme.h>
+
 #include "util/mem.h"
 
 #define __cleanup__(fn) __attribute__((cleanup(fn)))
@@ -34,4 +36,11 @@ static inline void close_file(int *f)
 }
 #define _cleanup_file_ __cleanup__(close_file)
 
+static inline void cleanup_nvme_root(nvme_root_t *r)
+{
+       if (r)
+               nvme_free_tree(*r);
+}
+#define _cleanup_nvme_root_ __cleanup__(cleanup_nvme_root)
+
 #endif