]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme: coverity fixes
authorHannes Reinecke <hare@suse.de>
Fri, 22 Apr 2022 11:20:30 +0000 (13:20 +0200)
committerHannes Reinecke <hare@suse.de>
Fri, 22 Apr 2022 11:20:30 +0000 (13:20 +0200)
Coverity found some logic issues and resource leaks.

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

index d0ddb53882610af2874217b1fad4563a8476b423..774cea6fdb2eee58b0e1d88d43ceb8eee4ed42d5 100644 (file)
@@ -1477,7 +1477,7 @@ static void nvme_show_persistent_event_log_rci(__le32 pel_header_rci)
        printf("\tReporting Context Port Identifier Type (RCPIT): %u(%s)\n", rcpit,
                (rcpit == 0x00) ? "Does not already exist" :
                (rcpit == 0x01) ? "NVM subsystem port" :
-               (rcpit == 0x10) ? "NVMe-MI port" : "Reserved");
+               (rcpit == 0x02) ? "NVMe-MI port" : "Reserved");
        printf("\tReporting Context Port Identifier (RCPID): %#x\n\n", rcpid);
 }
 
@@ -1490,8 +1490,8 @@ static void nvme_show_persistent_event_entry_ehai(__u8 ehai)
        printf("\tPort Identifier Type (PIT): %u(%s)\n", pit,
                (pit == 0x00) ? "PIT not reported and PELPID does not apply" :
                (pit == 0x01) ? "NVM subsystem port" :
-               (pit == 0x10) ? "NVMe-MI port" :
-               (pit == 0x11) ? "Event not associated with any port and PELPID does not apply" : "Reserved");
+               (pit == 0x02) ? "NVMe-MI port" :
+               "Event not associated with any port and PELPID does not apply");
 }
 
 void nvme_show_persistent_event_log(void *pevent_log_info,
@@ -1737,13 +1737,12 @@ void nvme_show_persistent_event_log(void *pevent_log_info,
                        fid = le32_to_cpu(set_feat_event->cdw_mem[0]) & 0x000f;
                        cdw11 = le32_to_cpu(set_feat_event->cdw_mem[1]);
 
-                       if (((set_feat_event->layout & 0xff) >> 2) != 0)
-                               mem_buf = (unsigned char *)(set_feat_event + 4 + dword_cnt * 4);
-
                        printf("Set Feature ID  :%#02x (%s),  value:%#08x\n", fid,
                                nvme_feature_to_string(fid), cdw11);
-
-                       nvme_feature_show_fields(fid, cdw11, mem_buf);
+                       if (((set_feat_event->layout & 0xff) >> 2) != 0) {
+                               mem_buf = (unsigned char *)(set_feat_event + 4 + dword_cnt * 4);
+                               nvme_feature_show_fields(fid, cdw11, mem_buf);
+                       }
                        break;
                case NVME_PEL_TELEMETRY_CRT:
                        d(pevent_log_info + offset, 512, 16, 1);
@@ -4362,7 +4361,7 @@ static void json_nvme_id_ns_descs(void *data)
 
                case NVME_NIDT_CSI:
                        memcpy(&desc.csi, data + off, sizeof(desc.csi));
-                       json_str_p += sprintf(json_str_p, "%#x", desc.csi);
+                       sprintf(json_str_p, "%#x", desc.csi);
                        len += sizeof(desc.csi);
                        nidt_name = "csi";
                        break;
diff --git a/nvme.c b/nvme.c
index 4533413487342c5bda526ba6011dcdc1e9ecc8a8..cfee960c0be07d01597a6c820b3bdcc3b50cc6c2 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -3173,7 +3173,7 @@ static int id_ns_granularity(int argc, char **argv, struct command *cmd, struct
                nvme_show_status(err);
        else
                fprintf(stderr, "identify namespace granularity: %s\n", nvme_strerror(errno));
-
+       free(granularity_list);
 close_fd:
        close(fd);
 ret:
@@ -3766,7 +3766,7 @@ static void get_feature_id_print(struct feat_cfg cfg, int err, __u32 result,
                               result);
                        if (cfg.sel == 3)
                                nvme_show_select_result(result);
-                       else if (cfg.human_readable)
+                       else if (cfg.human_readable && buf)
                                nvme_feature_show_fields(cfg.feature_id, result,
                                                         buf);
                        else if (buf)
@@ -5028,7 +5028,8 @@ static int set_feature(int argc, char **argv, struct command *cmd, struct plugin
                nvme_show_status(err);
 
 close_ffd:
-       close(ffd);
+       if (ffd != STDIN_FILENO)
+               close(ffd);
 free:
        free(buf);
 close_fd:
@@ -6202,7 +6203,7 @@ static int submit_io(int opcode, char *command, const char *desc,
        __u16 control = 0;
        __u32 dsmgmt = 0;
        int logical_block_size = 0;
-       long long buffer_size = 0, mbuffer_size = 0;
+       unsigned long long buffer_size = 0, mbuffer_size = 0;
        bool huge;
        struct nvme_id_ns ns;
        __u8 lba_index, ms = 0;
@@ -6394,7 +6395,7 @@ static int submit_io(int opcode, char *command, const char *desc,
                                        &logical_block_size) < 0)
                        goto close_mfd;
 
-       buffer_size = (cfg.block_count + 1) * logical_block_size;
+       buffer_size = ((long long)cfg.block_count + 1) * logical_block_size;
        if (cfg.data_size < buffer_size) {
                fprintf(stderr, "Rounding data size to fit block count (%lld bytes)\n",
                                buffer_size);
@@ -6410,7 +6411,7 @@ static int submit_io(int opcode, char *command, const char *desc,
 
        if (cfg.metadata_size) {
                err = nvme_identify_ns(fd, cfg.namespace_id, &ns);
-               if (err) {
+               if (err > 0) {
                        nvme_show_status(err);
                        goto free_buffer;
                } else if (err < 0) {
@@ -6419,7 +6420,7 @@ static int submit_io(int opcode, char *command, const char *desc,
                }
                nvme_id_ns_flbas_to_lbaf_inuse(ns.flbas, &lba_index);
                ms = ns.lbaf[lba_index].ms;
-               mbuffer_size = (cfg.block_count + 1) * ms;
+               mbuffer_size = ((unsigned long long)cfg.block_count + 1) * ms;
                if (ms && cfg.metadata_size < mbuffer_size) {
                        fprintf(stderr, "Rounding metadata size to fit block count (%lld bytes)\n",
                                        mbuffer_size);
@@ -7434,7 +7435,7 @@ static int passthru(int argc, char **argv, bool admin,
                                admin ? "Admin": "IO",
                                strcmp(cmd_name, "Unknown") ? cmd_name: "Vendor Specific",
                                result);
-               if (cfg.read && cfg.input_file) {
+               if (cfg.read && strlen(cfg.input_file)) {
                        if (write(dfd, (void *)data, cfg.data_len) < 0)
                                perror("failed to write data buffer");
                        if (cfg.metadata_len && cfg.metadata)