From: Alexey Timofeyev Date: Tue, 22 May 2018 18:31:29 +0000 (+0000) Subject: nvme-cli: Fix build breaks for GCC 8.1 X-Git-Tag: v1.6~43^2^2 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=09f64c5532bf99d16282ad470cd34b84827c4b53;p=users%2Fsagi%2Fnvme-cli.git nvme-cli: Fix build breaks for GCC 8.1 Signed-off-by: Alexey Timofeyev --- diff --git a/nvme-lightnvm.c b/nvme-lightnvm.c index 0fdc74d9..12a0b01d 100644 --- a/nvme-lightnvm.c +++ b/nvme-lightnvm.c @@ -64,8 +64,8 @@ int lnvm_do_init(char *dev, char *mmtype) return fd; memset(&init, 0, sizeof(struct nvm_ioctl_dev_init)); - strncpy(init.dev, dev, DISK_NAME_LEN); - strncpy(init.mmtype, mmtype, NVM_MMTYPE_LEN); + strncpy(init.dev, dev, DISK_NAME_LEN - 1); + strncpy(init.mmtype, mmtype, NVM_MMTYPE_LEN - 1); ret = ioctl(fd, NVM_DEV_INIT, &init); switch (errno) { @@ -157,9 +157,9 @@ int lnvm_do_create_tgt(char *devname, char *tgtname, char *tgttype, if (fd < 0) return fd; - strncpy(c.dev, devname, DISK_NAME_LEN); - strncpy(c.tgtname, tgtname, DISK_NAME_LEN); - strncpy(c.tgttype, tgttype, NVM_TTYPE_NAME_MAX); + strncpy(c.dev, devname, DISK_NAME_LEN - 1); + strncpy(c.tgtname, tgtname, DISK_NAME_LEN - 1); + strncpy(c.tgttype, tgttype, NVM_TTYPE_NAME_MAX - 1); c.flags = flags; /* Fall back into simple IOCTL version if no extended attributes used */ @@ -191,7 +191,7 @@ int lnvm_do_remove_tgt(char *tgtname) if (fd < 0) return fd; - strncpy(c.tgtname, tgtname, DISK_NAME_LEN); + strncpy(c.tgtname, tgtname, DISK_NAME_LEN - 1); c.flags = 0; ret = ioctl(fd, NVM_DEV_REMOVE, &c); @@ -215,7 +215,7 @@ int lnvm_do_factory_init(char *devname, int erase_only_marked, memset(&fact, 0, sizeof(struct nvm_ioctl_dev_factory)); - strncpy(fact.dev, devname, DISK_NAME_LEN); + strncpy(fact.dev, devname, DISK_NAME_LEN - 1); if (erase_only_marked) fact.flags |= NVM_FACTORY_ERASE_ONLY_USER; if (clear_host_marks) diff --git a/wdc-nvme.c b/wdc-nvme.c index 0f4375c3..c480f025 100644 --- a/wdc-nvme.c +++ b/wdc-nvme.c @@ -508,11 +508,13 @@ static int wdc_get_serial_name(int fd, char *file, size_t len, char *suffix) { int i; int ret; + int ctrl_sn_len = 20; + int res_len = 0; char orig[PATH_MAX] = {0}; struct nvme_id_ctrl ctrl; i = sizeof (ctrl.sn) - 1; - strncpy(orig, file, PATH_MAX); + strncpy(orig, file, PATH_MAX - 1); memset(file, 0, len); memset(&ctrl, 0, sizeof (struct nvme_id_ctrl)); ret = nvme_identify_ctrl(fd, &ctrl); @@ -526,7 +528,18 @@ static int wdc_get_serial_name(int fd, char *file, size_t len, char *suffix) ctrl.sn[i] = '\0'; i--; } - snprintf(file, len, "%s%s%s.bin", orig, ctrl.sn, suffix); + + if (ctrl.sn[sizeof (ctrl.sn) - 1] == '\0') { + ctrl_sn_len = strlen(ctrl.sn); + } + + res_len = snprintf(file, len, "%s%.*s%s.bin", orig, ctrl_sn_len, ctrl.sn, suffix); + if (len <= res_len) { + fprintf(stderr, "ERROR : WDC : cannot format serial number due to data " + "of unexpected length\n"); + return -1; + } + return 0; } @@ -770,7 +783,7 @@ static int wdc_cap_diag(int argc, char **argv, struct command *command, wdc_check_device(fd); if (cfg.file != NULL) { - strncpy(f, cfg.file, PATH_MAX); + strncpy(f, cfg.file, PATH_MAX - 1); } if (wdc_get_serial_name(fd, f, PATH_MAX, "cap_diag") == -1) { fprintf(stderr, "ERROR : WDC: failed to generate file name\n"); @@ -813,7 +826,7 @@ static int wdc_crash_dump(int fd, char *file) char f[PATH_MAX] = {0}; if (file != NULL) { - strncpy(f, file, PATH_MAX); + strncpy(f, file, PATH_MAX - 1); } if (wdc_get_serial_name(fd, f, PATH_MAX, "crash_dump") == -1) { fprintf(stderr, "ERROR : WDC : failed to generate file name\n"); @@ -890,7 +903,7 @@ static int wdc_drive_log(int argc, char **argv, struct command *command, wdc_check_device(fd); if (cfg.file != NULL) { - strncpy(f, cfg.file, PATH_MAX); + strncpy(f, cfg.file, PATH_MAX - 1); } if (wdc_get_serial_name(fd, f, PATH_MAX, "drive_log") == -1) { fprintf(stderr, "ERROR : WDC : failed to generate file name\n"); @@ -2250,7 +2263,7 @@ static int wdc_drive_essentials(int argc, char **argv, struct command *command, } if (cfg.dirName != NULL) { - strncpy(d, cfg.dirName, PATH_MAX); + strncpy(d, cfg.dirName, PATH_MAX - 1); d_ptr = d; } else { d_ptr = NULL;