]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme-cli: Fix build breaks for GCC 8.1
authorAlexey Timofeyev <alexey.timofeyev@sk.com>
Tue, 22 May 2018 18:31:29 +0000 (18:31 +0000)
committerAlexey Timofeyev <alexey.timofeyev@sk.com>
Tue, 22 May 2018 18:31:29 +0000 (18:31 +0000)
Signed-off-by: Alexey Timofeyev <alexey.timofeyev@sk.com>
nvme-lightnvm.c
wdc-nvme.c

index 0fdc74d999120da5f4d08d8a7d34137566e781f5..12a0b01d8bf0f21706fd6758b034ed8a3ccf802a 100644 (file)
@@ -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)
index 0f4375c37d1efad2b90a4cdc35a8cca43f58b8c8..c480f025d5f904cbf4e1e95f7fdbf5d992475469 100644 (file)
@@ -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;