From f5ecab3aef3db6744f135daf8104dbb1880cc1eb Mon Sep 17 00:00:00 2001 From: Tony Hutter Date: Wed, 4 Apr 2018 10:16:47 -0700 Subject: Fedora 28: Fix misc bounds check compiler warnings Fix a bunch of (mostly) sprintf/snprintf truncation compiler warnings that show up on Fedora 28 (GCC 8.0.1). Reviewed-by: Brian Behlendorf Signed-off-by: Tony Hutter Closes #7361 Closes #7368 --- tests/zfs-tests/cmd/devname2devid/devname2devid.c | 3 +- tests/zfs-tests/cmd/mkbusy/mkbusy.c | 16 ++++----- tests/zfs-tests/cmd/mktree/mktree.c | 8 +++-- tests/zfs-tests/cmd/xattrtest/xattrtest.c | 41 ++++++++++++++++++----- 4 files changed, 47 insertions(+), 21 deletions(-) (limited to 'tests') diff --git a/tests/zfs-tests/cmd/devname2devid/devname2devid.c b/tests/zfs-tests/cmd/devname2devid/devname2devid.c index 59fbcdddb..91e59c589 100644 --- a/tests/zfs-tests/cmd/devname2devid/devname2devid.c +++ b/tests/zfs-tests/cmd/devname2devid/devname2devid.c @@ -83,7 +83,8 @@ udev_device_get_devid(struct udev_device *dev, char *bufptr, size_t buflen) name = udev_list_entry_get_name(entry); if (strncmp(name, devbyid, strlen(devbyid)) == 0) { name += strlen(DEV_BYID_PATH); - (void) stpncpy(bufptr, name, buflen); + (void) stpncpy(bufptr, name, buflen - 1); + bufptr[buflen - 1] = '\0'; return (0); } entry = udev_list_entry_get_next(entry); diff --git a/tests/zfs-tests/cmd/mkbusy/mkbusy.c b/tests/zfs-tests/cmd/mkbusy/mkbusy.c index 1e4567488..9634904f0 100644 --- a/tests/zfs-tests/cmd/mkbusy/mkbusy.c +++ b/tests/zfs-tests/cmd/mkbusy/mkbusy.c @@ -98,8 +98,9 @@ main(int argc, char *argv[]) if ((ret = stat(argv[0], &sbuf)) != 0) { char *arg, *dname, *fname; - int arglen, dlen, flen; + int arglen; char *slash; + int rc; /* * The argument supplied doesn't exist. Copy the path, and @@ -126,23 +127,18 @@ main(int argc, char *argv[]) free(arg); if (dname == NULL || fname == NULL) fail("strdup", 1); - dlen = strlen(dname); - flen = strlen(fname); /* The directory portion of the path must exist */ if ((ret = stat(dname, &sbuf)) != 0 || !(sbuf.st_mode & S_IFDIR)) usage(prog); - if ((fpath = (char *)malloc(dlen + 1 + flen + 1)) == NULL) - fail("malloc", 1); - (void) memset(fpath, '\0', dlen + 1 + flen + 1); - - (void) strncpy(fpath, dname, dlen); - fpath[dlen] = '/'; - (void) strncat(fpath, fname, flen); + rc = asprintf(&fpath, "%s/%s", dname, fname); free(dname); free(fname); + if (rc == -1 || fpath == NULL) + fail("asprintf", 1); + } else if ((sbuf.st_mode & S_IFMT) == S_IFREG || (sbuf.st_mode & S_IFMT) == S_IFLNK || (sbuf.st_mode & S_IFMT) == S_IFCHR || diff --git a/tests/zfs-tests/cmd/mktree/mktree.c b/tests/zfs-tests/cmd/mktree/mktree.c index bf0ec5e0c..02d4974d7 100644 --- a/tests/zfs-tests/cmd/mktree/mktree.c +++ b/tests/zfs-tests/cmd/mktree/mktree.c @@ -137,8 +137,12 @@ mktree(char *pdir, int level) static char * getfdname(char *pdir, char type, int level, int dir, int file) { - (void) snprintf(fdname, sizeof (fdname), - "%s/%c-l%dd%df%d", pdir, type, level, dir, file); + size_t size = sizeof (fdname); + if (snprintf(fdname, size, "%s/%c-l%dd%df%d", pdir, type, level, dir, + file) >= size) { + (void) fprintf(stderr, "fdname truncated\n"); + exit(EINVAL); + } return (fdname); } diff --git a/tests/zfs-tests/cmd/xattrtest/xattrtest.c b/tests/zfs-tests/cmd/xattrtest/xattrtest.c index dd3f2a6c9..32a6b1d95 100644 --- a/tests/zfs-tests/cmd/xattrtest/xattrtest.c +++ b/tests/zfs-tests/cmd/xattrtest/xattrtest.c @@ -367,8 +367,10 @@ create_files(void) char *file = NULL; struct timeval start, stop; double seconds; + size_t fsize; - file = malloc(PATH_MAX); + fsize = PATH_MAX; + file = malloc(fsize); if (file == NULL) { rc = ENOMEM; ERROR("Error %d: malloc(%d) bytes for file name\n", rc, @@ -379,7 +381,11 @@ create_files(void) (void) gettimeofday(&start, NULL); for (i = 1; i <= files; i++) { - (void) sprintf(file, "%s/file-%d", path, i); + if (snprintf(file, fsize, "%s/file-%d", path, i) >= fsize) { + rc = EINVAL; + ERROR("Error %d: path too long\n", rc); + goto out; + } if (nth && ((i % nth) == 0)) fprintf(stdout, "create: %s\n", file); @@ -452,6 +458,7 @@ setxattrs(void) char *file = NULL; struct timeval start, stop; double seconds; + size_t fsize; value = malloc(XATTR_SIZE_MAX); if (value == NULL) { @@ -461,7 +468,8 @@ setxattrs(void) goto out; } - file = malloc(PATH_MAX); + fsize = PATH_MAX; + file = malloc(fsize); if (file == NULL) { rc = ENOMEM; ERROR("Error %d: malloc(%d) bytes for file name\n", rc, @@ -472,7 +480,11 @@ setxattrs(void) (void) gettimeofday(&start, NULL); for (i = 1; i <= files; i++) { - (void) sprintf(file, "%s/file-%d", path, i); + if (snprintf(file, fsize, "%s/file-%d", path, i) >= fsize) { + rc = EINVAL; + ERROR("Error %d: path too long\n", rc); + goto out; + } if (nth && ((i % nth) == 0)) fprintf(stdout, "setxattr: %s\n", file); @@ -523,6 +535,7 @@ getxattrs(void) char *file = NULL; struct timeval start, stop; double seconds; + size_t fsize; verify_value = malloc(XATTR_SIZE_MAX); if (verify_value == NULL) { @@ -543,7 +556,9 @@ getxattrs(void) verify_string = value_is_random ? "" : verify_value; value_string = value_is_random ? "" : value; - file = malloc(PATH_MAX); + fsize = PATH_MAX; + file = malloc(fsize); + if (file == NULL) { rc = ENOMEM; ERROR("Error %d: malloc(%d) bytes for file name\n", rc, @@ -554,7 +569,11 @@ getxattrs(void) (void) gettimeofday(&start, NULL); for (i = 1; i <= files; i++) { - (void) sprintf(file, "%s/file-%d", path, i); + if (snprintf(file, fsize, "%s/file-%d", path, i) >= fsize) { + rc = EINVAL; + ERROR("Error %d: path too long\n", rc); + goto out; + } if (nth && ((i % nth) == 0)) fprintf(stdout, "getxattr: %s\n", file); @@ -615,8 +634,10 @@ unlink_files(void) char *file = NULL; struct timeval start, stop; double seconds; + size_t fsize; - file = malloc(PATH_MAX); + fsize = PATH_MAX; + file = malloc(fsize); if (file == NULL) { rc = ENOMEM; ERROR("Error %d: malloc(%d) bytes for file name\n", @@ -627,7 +648,11 @@ unlink_files(void) (void) gettimeofday(&start, NULL); for (i = 1; i <= files; i++) { - (void) sprintf(file, "%s/file-%d", path, i); + if (snprintf(file, fsize, "%s/file-%d", path, i) >= fsize) { + rc = EINVAL; + ERROR("Error %d: path too long\n", rc); + goto out; + } if (nth && ((i % nth) == 0)) fprintf(stdout, "unlink: %s\n", file); -- cgit v1.2.3