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
This commit is contained in:
David Bright 2020-04-02 13:52:54 +00:00
parent 3b8de63481
commit 6995fb5eb7
8 changed files with 27 additions and 11 deletions

View File

@ -151,6 +151,7 @@ read_image_file(const char *path, void **buf, int32_t *size)
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 payload_size)
resid -= size;
off += size;
}
free(chunk);
}
static int

View File

@ -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);

View File

@ -570,11 +570,11 @@ print_log_sanitize_status(const struct nvme_controller_data *cdata __unused,
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");

View File

@ -275,7 +275,7 @@ print_hgst_info_subpage_gen(void *buf, uint16_t subtype __unused, uint32_t size,
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);
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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");

View File

@ -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)