Author: dab
Date: Thu Apr  2 13:52:54 2020
New Revision: 359562
URL: https://svnweb.freebsd.org/changeset/base/359562

Log:
  Fix various Coverity-detected errors in nvmecontrol
  
  This fixes several Coverity-detected errors in nvmecontrol. While in
  here, a couple additional errors with shift/mask confusion that were
  not diagnosed by Coverity are also fixed.
  
  CIDs addressed: 1040299, 1040300, 1403972, 1403973, 1403985, 1403988,
  1403990, 1404374, 1404427, 1404469, 1404510, 1404534, 1418118
  
  CID 1403657 (resource leak of shared library handle) was marked
  "intentional" in the Coverity scan database.
  
  Reviewed by:  vangyzen, robert.herndon_dell.com
  Reviewed by:  daniel.william.ryan_gmail.com (earlier version)
  Reviewed by:  rramsden_isilon.com (earlier version), imp
  MFC after:    5 days
  Sponsored by: Dell EMC Isilon
  Differential Revision:        https://reviews.freebsd.org/D24203

Modified:
  head/sbin/nvmecontrol/firmware.c
  head/sbin/nvmecontrol/identify.c
  head/sbin/nvmecontrol/logpage.c
  head/sbin/nvmecontrol/modules/wdc/wdc.c
  head/sbin/nvmecontrol/nsid.c
  head/sbin/nvmecontrol/passthru.c
  head/sbin/nvmecontrol/power.c
  head/sbin/nvmecontrol/reset.c

Modified: head/sbin/nvmecontrol/firmware.c
==============================================================================
--- head/sbin/nvmecontrol/firmware.c    Thu Apr  2 11:08:19 2020        
(r359561)
+++ head/sbin/nvmecontrol/firmware.c    Thu Apr  2 13:52:54 2020        
(r359562)
@@ -151,6 +151,7 @@ read_image_file(const char *path, void **buf, int32_t 
                errx(1,
                    "error reading '%s' (read %d bytes, requested %d bytes)",
                    path, *size, filesize);
+       close(fd);
 }
 
 static void
@@ -188,6 +189,7 @@ update_firmware(int fd, uint8_t *payload, int32_t payl
                resid -= size;
                off += size;
        }
+       free(chunk);
 }
 
 static int

Modified: head/sbin/nvmecontrol/identify.c
==============================================================================
--- head/sbin/nvmecontrol/identify.c    Thu Apr  2 11:08:19 2020        
(r359561)
+++ head/sbin/nvmecontrol/identify.c    Thu Apr  2 13:52:54 2020        
(r359562)
@@ -94,7 +94,7 @@ print_namespace(struct nvme_namespace_data *nsdata)
             NVME_NS_DATA_DPC_PIT3_MASK) ? "Type 3, " : "",
            ((nsdata->dpc >> NVME_NS_DATA_DPC_PIT2_SHIFT) &
             NVME_NS_DATA_DPC_PIT2_MASK) ? "Type 2, " : "",
-           ((nsdata->dpc >> NVME_NS_DATA_DPC_PIT2_MASK) &
+           ((nsdata->dpc >> NVME_NS_DATA_DPC_PIT1_SHIFT) &
             NVME_NS_DATA_DPC_PIT1_MASK) ? "Type 1" : "");
        printf("Data Protection Settings:    ");
        ptype = (nsdata->dps >> NVME_NS_DATA_DPS_PIT_SHIFT) &
@@ -238,7 +238,8 @@ identify(const struct cmd *f, int argc, char *argv[])
        int             fd;
        uint32_t        nsid;
 
-       arg_parse(argc, argv, f);
+       if (arg_parse(argc, argv, f))
+               return;
 
        open_dev(opt.dev, &fd, 1, 1);
        get_nsid(fd, &path, &nsid);

Modified: head/sbin/nvmecontrol/logpage.c
==============================================================================
--- head/sbin/nvmecontrol/logpage.c     Thu Apr  2 11:08:19 2020        
(r359561)
+++ head/sbin/nvmecontrol/logpage.c     Thu Apr  2 13:52:54 2020        
(r359562)
@@ -570,11 +570,11 @@ print_log_sanitize_status(const struct nvme_controller
                printf("Unknown");
                break;
        }
-       p = (ss->sstat & NVME_SS_PAGE_SSTAT_PASSES_SHIFT) >>
+       p = (ss->sstat >> NVME_SS_PAGE_SSTAT_PASSES_SHIFT) &
            NVME_SS_PAGE_SSTAT_PASSES_MASK;
        if (p > 0)
                printf(", %d passes", p);
-       if ((ss->sstat & NVME_SS_PAGE_SSTAT_GDE_SHIFT) >>
+       if ((ss->sstat >> NVME_SS_PAGE_SSTAT_GDE_SHIFT) &
            NVME_SS_PAGE_SSTAT_GDE_MASK)
                printf(", Global Data Erased");
        printf("\n");

Modified: head/sbin/nvmecontrol/modules/wdc/wdc.c
==============================================================================
--- head/sbin/nvmecontrol/modules/wdc/wdc.c     Thu Apr  2 11:08:19 2020        
(r359561)
+++ head/sbin/nvmecontrol/modules/wdc/wdc.c     Thu Apr  2 13:52:54 2020        
(r359562)
@@ -275,7 +275,7 @@ print_hgst_info_subpage_gen(void *buf, uint16_t subtyp
                wsp++;                  /* Flags, just ignore */
                plen = *wsp++;
                param = 0;
-               for (i = 0; i < plen; i++)
+               for (i = 0; i < plen && wsp < esp; i++)
                        param |= (uint64_t)*wsp++ << (i * 8);
                printf("  %-30s: %jd\n", kv_lookup(kv, kv_count, ptype), 
(uintmax_t)param);
        }

Modified: head/sbin/nvmecontrol/nsid.c
==============================================================================
--- head/sbin/nvmecontrol/nsid.c        Thu Apr  2 11:08:19 2020        
(r359561)
+++ head/sbin/nvmecontrol/nsid.c        Thu Apr  2 13:52:54 2020        
(r359562)
@@ -70,7 +70,8 @@ gnsid(const struct cmd *f, int argc, char *argv[])
        int             fd;
        uint32_t        nsid;
 
-       arg_parse(argc, argv, f);
+       if (arg_parse(argc, argv, f))
+               return;
 
        open_dev(nsid_opt.dev, &fd, 1, 1);
        get_nsid(fd, &path, &nsid);

Modified: head/sbin/nvmecontrol/passthru.c
==============================================================================
--- head/sbin/nvmecontrol/passthru.c    Thu Apr  2 11:08:19 2020        
(r359561)
+++ head/sbin/nvmecontrol/passthru.c    Thu Apr  2 13:52:54 2020        
(r359562)
@@ -158,10 +158,12 @@ static void
 passthru(const struct cmd *f, int argc, char *argv[])
 {
        int     fd = -1, ifd = -1;
+       size_t  bytes_read;
        void    *data = NULL, *metadata = NULL;
        struct nvme_pt_command  pt;
 
-       arg_parse(argc, argv, f);
+       if (arg_parse(argc, argv, f))
+               return;
        open_dev(argv[optind], &fd, 1, 1);
 
        if (opt.read && opt.write)
@@ -189,8 +191,12 @@ passthru(const struct cmd *f, int argc, char *argv[])
                        goto cleanup;
                }
                memset(data, opt.prefill, opt.data_len);
-               if (opt.write && read(ifd, data, opt.data_len) < 0) {
-                       warn("read %s", *opt.ifn ? opt.ifn : "stdin");
+               if (opt.write &&
+                   (bytes_read = read(ifd, data, opt.data_len)) !=
+                   opt.data_len) {
+                       warn("read %s; expected %u bytes; got %zd",
+                            *opt.ifn ? opt.ifn : "stdin",
+                            opt.data_len, bytes_read);
                        goto cleanup;
                }
        }
@@ -249,6 +255,10 @@ passthru(const struct cmd *f, int argc, char *argv[])
                }
        }
 cleanup:
+       free(data);
+       close(fd);
+       if (ifd > -1)
+               close(ifd);
        if (errno)
                exit(1);
 }

Modified: head/sbin/nvmecontrol/power.c
==============================================================================
--- head/sbin/nvmecontrol/power.c       Thu Apr  2 11:08:19 2020        
(r359561)
+++ head/sbin/nvmecontrol/power.c       Thu Apr  2 13:52:54 2020        
(r359562)
@@ -145,7 +145,8 @@ power(const struct cmd *f, int argc, char *argv[])
        struct nvme_controller_data     cdata;
        int                             fd;
 
-       arg_parse(argc, argv, f);
+       if (arg_parse(argc, argv, f))
+               return;
 
        if (opt.list && opt.power != POWER_NONE) {
                fprintf(stderr, "Can't set power and list power states\n");

Modified: head/sbin/nvmecontrol/reset.c
==============================================================================
--- head/sbin/nvmecontrol/reset.c       Thu Apr  2 11:08:19 2020        
(r359561)
+++ head/sbin/nvmecontrol/reset.c       Thu Apr  2 13:52:54 2020        
(r359562)
@@ -57,7 +57,8 @@ reset(const struct cmd *f, int argc, char *argv[])
 {
        int     fd;
 
-       arg_parse(argc, argv, f);
+       if (arg_parse(argc, argv, f))
+               return;
        open_dev(opt.dev, &fd, 1, 1);
 
        if (ioctl(fd, NVME_RESET_CONTROLLER) < 0)
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to