From: fixthething <79883270+fixthething@users.noreply.github.com> Date: Sun, 14 Mar 2021 00:02:00 +0000 (-0500) Subject: Changes requested by keithbusch; no default for cfg.tl. X-Git-Tag: v1.15~3^2~1 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=1b6fbfaea68eb60f2ec527656c3ecb7e3669449a;p=users%2Fsagi%2Fnvme-cli.git Changes requested by keithbusch; no default for cfg.tl. --- diff --git a/nvme.c b/nvme.c index ad127b9b..9b95cf66 100644 --- a/nvme.c +++ b/nvme.c @@ -3614,8 +3614,7 @@ static int sec_send(int argc, char **argv, struct command *cmd, struct plugin *p const char *nssf = "NVMe Security Specific Field"; int err, fd, sec_fd = -1; void *sec_buf; - unsigned int sec_size, buf_size; - int nread = 0; + unsigned int sec_size; struct config { __u32 namespace_id; @@ -3647,13 +3646,17 @@ static int sec_send(int argc, char **argv, struct command *cmd, struct plugin *p if (fd < 0) goto ret; + if (cfg.tl == 0) { + fprintf(stderr, "--tl unspecified or zero\n"); + err = -EINVAL; + goto close_fd; + } + if ((cfg.tl & 3) != 0) + fprintf(stderr, "WARNING: --tl not dword aligned; unaligned bytes may be truncated\n"); + if (strlen(cfg.file) == 0) { sec_fd = STDIN_FILENO; - - if (cfg.tl || ioctl(sec_fd, FIONREAD, &nread) < 0) - sec_size = cfg.tl; - else - sec_size = (unsigned int) nread; + sec_size = cfg.tl; } else { sec_fd = open(cfg.file, O_RDONLY); if (sec_fd < 0) { @@ -3669,19 +3672,16 @@ static int sec_send(int argc, char **argv, struct command *cmd, struct plugin *p goto close_sec_fd; } - sec_size = sb.st_size; + sec_size = cfg.tl > sb.st_size ? cfg.tl : sb.st_size; } - buf_size = cfg.tl ? cfg.tl : sec_size; - buf_size = (buf_size + 3) & ~3; // dword align to avoid truncation - - if (posix_memalign(&sec_buf, getpagesize(), buf_size)) { - fprintf(stderr, "No memory for security size:%d\n", buf_size); + if (posix_memalign(&sec_buf, getpagesize(), cfg.tl)) { + fprintf(stderr, "No memory for security size:%d\n", cfg.tl); err = -ENOMEM; goto close_sec_fd; } - memset(sec_buf, 0, buf_size); // ensure zero fill if buf_size > sec_size + memset(sec_buf, 0, cfg.tl); // ensure zero fill if buf_size > sec_size err = read(sec_fd, sec_buf, sec_size); if (err < 0) { @@ -3692,7 +3692,7 @@ static int sec_send(int argc, char **argv, struct command *cmd, struct plugin *p } err = nvme_sec_send(fd, cfg.namespace_id, cfg.nssf, cfg.spsp, cfg.secp, - buf_size, buf_size, sec_buf); + cfg.tl, cfg.tl, sec_buf); if (err < 0) perror("security-send"); else if (err != 0)