]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme-models: fixup resource leaks
authorHannes Reinecke <hare@suse.de>
Fri, 22 Apr 2022 11:19:02 +0000 (13:19 +0200)
committerHannes Reinecke <hare@suse.de>
Fri, 22 Apr 2022 11:19:02 +0000 (13:19 +0200)
Found by coverity.

Signed-off-by: Hannes Reinecke <hare@suse.de>
nvme-models.c

index 8fd815202cd8b93488c89b7d707806e230a8a2c2..bd81f3671a3e2f283dc5b3c08acf07b1d796e6ee 100644 (file)
@@ -59,10 +59,13 @@ static char *find_data(char *data)
 static char *locate_info(char *data, bool is_inner, bool is_class)
 {
        char *orig = data;
-       char *locate = find_data(data);
+       char *locate;
        if (!data)
                return orig;
 
+       locate = find_data(data);
+       if (!locate)
+               return orig;
        if (is_class)
                return locate + 4;
        if (!is_inner)
@@ -236,7 +239,7 @@ static void pull_class_info(char **_newline, FILE *file, char *class)
 static int read_sys_node(char *where, char *save, size_t savesz)
 {
        char *new;
-       int fd, ret = 0;
+       int fd, ret = 0, len;
        fd = open(where, O_RDONLY);
        if (fd < 0) {
                fprintf(stderr, "Failed to open %s with errno %s\n",
@@ -244,13 +247,15 @@ static int read_sys_node(char *where, char *save, size_t savesz)
                return 1;
        }
        /* -1 so we can safely use strstr below */
-       if(!read(fd, save, savesz - 1))
+       len = read(fd, save, savesz - 1);
+       if (!len)
                ret = 1;
-
-       new = strstr(save, "\n");
-       if (new)
-               new[0] = '\0';
-
+       else {
+               save[len] = '\0';
+               new = strstr(save, "\n");
+               if (new)
+                       new[0] = '\0';
+       }
        close(fd);
        return ret;
 }
@@ -330,6 +335,7 @@ char *nvme_product_name(int id)
                        continue;
                if (is_top_level_match(line, vendor, false)) {
                        line[amnt - 1] = '\0';
+                       free(device_top);
                        device_top = strdup(line);
                        parse_vendor_device(&line, file,
                                                device,
@@ -346,5 +352,5 @@ char *nvme_product_name(int id)
 error0:
        fclose(file);
 error1:
-       return !line ? strdup("NULL") : strdup("Unknown Device");
+       return strdup("NULL");
 }