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 <behlendorf1@llnl.gov>
Signed-off-by: Tony Hutter <hutter2@llnl.gov>
Closes #7361
Closes #7368
This commit is contained in:
Tony Hutter 2018-04-04 10:16:47 -07:00
parent fd01167ffd
commit f5ecab3aef
10 changed files with 80 additions and 39 deletions

View File

@ -525,10 +525,11 @@ run_one(cmd_args_t *args, uint32_t id, uint32_t T, uint32_t N,
memset(cmd, 0, cmd_size); memset(cmd, 0, cmd_size);
cmd->cmd_magic = ZPIOS_CMD_MAGIC; cmd->cmd_magic = ZPIOS_CMD_MAGIC;
strncpy(cmd->cmd_pool, args->pool, ZPIOS_NAME_SIZE - 1); snprintf(cmd->cmd_pool, sizeof (cmd->cmd_pool), "%s", args->pool);
strncpy(cmd->cmd_pre, args->pre, ZPIOS_PATH_SIZE - 1); snprintf(cmd->cmd_pre, sizeof (cmd->cmd_pre), "%s", args->pre);
strncpy(cmd->cmd_post, args->post, ZPIOS_PATH_SIZE - 1); snprintf(cmd->cmd_post, sizeof (cmd->cmd_post), "%s", args->post);
strncpy(cmd->cmd_log, args->log, ZPIOS_PATH_SIZE - 1); snprintf(cmd->cmd_log, sizeof (cmd->cmd_log), "%s", args->log);
cmd->cmd_id = id; cmd->cmd_id = id;
cmd->cmd_chunk_size = C; cmd->cmd_chunk_size = C;
cmd->cmd_thread_count = T; cmd->cmd_thread_count = T;

View File

@ -55,11 +55,12 @@ main(int argc, char **argv)
{ {
int fd, error = 0; int fd, error = 0;
char zvol_name[ZFS_MAX_DATASET_NAME_LEN]; char zvol_name[ZFS_MAX_DATASET_NAME_LEN];
char zvol_name_part[ZFS_MAX_DATASET_NAME_LEN]; char *zvol_name_part = NULL;
char *dev_name; char *dev_name;
struct stat64 statbuf; struct stat64 statbuf;
int dev_minor, dev_part; int dev_minor, dev_part;
int i; int i;
int rc;
if (argc < 2) { if (argc < 2) {
printf("Usage: %s /dev/zvol_device_node\n", argv[0]); printf("Usage: %s /dev/zvol_device_node\n", argv[0]);
@ -88,11 +89,13 @@ main(int argc, char **argv)
return (errno); return (errno);
} }
if (dev_part > 0) if (dev_part > 0)
snprintf(zvol_name_part, ZFS_MAX_DATASET_NAME_LEN, rc = asprintf(&zvol_name_part, "%s-part%d", zvol_name,
"%s-part%d", zvol_name, dev_part); dev_part);
else else
snprintf(zvol_name_part, ZFS_MAX_DATASET_NAME_LEN, rc = asprintf(&zvol_name_part, "%s", zvol_name);
"%s", zvol_name);
if (rc == -1 || zvol_name_part == NULL)
goto error;
for (i = 0; i < strlen(zvol_name_part); i++) { for (i = 0; i < strlen(zvol_name_part); i++) {
if (isblank(zvol_name_part[i])) if (isblank(zvol_name_part[i]))
@ -100,6 +103,8 @@ main(int argc, char **argv)
} }
printf("%s\n", zvol_name_part); printf("%s\n", zvol_name_part);
free(zvol_name_part);
error:
close(fd); close(fd);
return (error); return (error);
} }

View File

@ -146,7 +146,7 @@ umem_cache_create(
cp = umem_alloc(sizeof (umem_cache_t), UMEM_DEFAULT); cp = umem_alloc(sizeof (umem_cache_t), UMEM_DEFAULT);
if (cp) { if (cp) {
strncpy(cp->cache_name, name, UMEM_CACHE_NAMELEN); strlcpy(cp->cache_name, name, UMEM_CACHE_NAMELEN);
cp->cache_bufsize = bufsize; cp->cache_bufsize = bufsize;
cp->cache_align = align; cp->cache_align = align;
cp->cache_constructor = constructor; cp->cache_constructor = constructor;

View File

@ -1030,10 +1030,11 @@ zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) { if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
zfs_userquota_prop_t uqtype; zfs_userquota_prop_t uqtype;
char newpropname[128]; char *newpropname = NULL;
char domain[128]; char domain[128];
uint64_t rid; uint64_t rid;
uint64_t valary[3]; uint64_t valary[3];
int rc;
if (userquota_propname_decode(propname, zoned, if (userquota_propname_decode(propname, zoned,
&uqtype, domain, sizeof (domain), &rid) != 0) { &uqtype, domain, sizeof (domain), &rid) != 0) {
@ -1088,17 +1089,24 @@ zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
* userquota@<hex-rid>-domain, to make it easy * userquota@<hex-rid>-domain, to make it easy
* for the kernel to decode. * for the kernel to decode.
*/ */
(void) snprintf(newpropname, sizeof (newpropname), rc = asprintf(&newpropname, "%s%llx-%s",
"%s%llx-%s", zfs_userquota_prop_prefixes[uqtype], zfs_userquota_prop_prefixes[uqtype],
(longlong_t)rid, domain); (longlong_t)rid, domain);
if (rc == -1 || newpropname == NULL) {
(void) no_memory(hdl);
goto error;
}
valary[0] = uqtype; valary[0] = uqtype;
valary[1] = rid; valary[1] = rid;
valary[2] = intval; valary[2] = intval;
if (nvlist_add_uint64_array(ret, newpropname, if (nvlist_add_uint64_array(ret, newpropname,
valary, 3) != 0) { valary, 3) != 0) {
free(newpropname);
(void) no_memory(hdl); (void) no_memory(hdl);
goto error; goto error;
} }
free(newpropname);
continue; continue;
} else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) { } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,

View File

@ -3380,7 +3380,7 @@ zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
* Determine the name of the origin snapshot. * Determine the name of the origin snapshot.
*/ */
if (originsnap) { if (originsnap) {
(void) strncpy(origin, originsnap, sizeof (origin)); (void) strlcpy(origin, originsnap, sizeof (origin));
if (flags->verbose) if (flags->verbose)
(void) printf("using provided clone origin %s\n", (void) printf("using provided clone origin %s\n",
origin); origin);

View File

@ -289,10 +289,11 @@ zpios_setup_run(run_args_t **run_args, zpios_cmd_t *kcmd, struct file *file)
ra = vmem_zalloc(size, KM_SLEEP); ra = vmem_zalloc(size, KM_SLEEP);
*run_args = ra; *run_args = ra;
strncpy(ra->pool, kcmd->cmd_pool, ZPIOS_NAME_SIZE - 1); snprintf(ra->pool, sizeof (ra->pool), "%s", kcmd->cmd_pool);
strncpy(ra->pre, kcmd->cmd_pre, ZPIOS_PATH_SIZE - 1); snprintf(ra->pre, sizeof (ra->pre), "%s", kcmd->cmd_pre);
strncpy(ra->post, kcmd->cmd_post, ZPIOS_PATH_SIZE - 1); snprintf(ra->post, sizeof (ra->post), "%s", kcmd->cmd_post);
strncpy(ra->log, kcmd->cmd_log, ZPIOS_PATH_SIZE - 1); snprintf(ra->log, sizeof (ra->log), "%s", kcmd->cmd_log);
ra->id = kcmd->cmd_id; ra->id = kcmd->cmd_id;
ra->chunk_size = kcmd->cmd_chunk_size; ra->chunk_size = kcmd->cmd_chunk_size;
ra->thread_count = kcmd->cmd_thread_count; ra->thread_count = kcmd->cmd_thread_count;

View File

@ -83,7 +83,8 @@ udev_device_get_devid(struct udev_device *dev, char *bufptr, size_t buflen)
name = udev_list_entry_get_name(entry); name = udev_list_entry_get_name(entry);
if (strncmp(name, devbyid, strlen(devbyid)) == 0) { if (strncmp(name, devbyid, strlen(devbyid)) == 0) {
name += strlen(DEV_BYID_PATH); name += strlen(DEV_BYID_PATH);
(void) stpncpy(bufptr, name, buflen); (void) stpncpy(bufptr, name, buflen - 1);
bufptr[buflen - 1] = '\0';
return (0); return (0);
} }
entry = udev_list_entry_get_next(entry); entry = udev_list_entry_get_next(entry);

View File

@ -98,8 +98,9 @@ main(int argc, char *argv[])
if ((ret = stat(argv[0], &sbuf)) != 0) { if ((ret = stat(argv[0], &sbuf)) != 0) {
char *arg, *dname, *fname; char *arg, *dname, *fname;
int arglen, dlen, flen; int arglen;
char *slash; char *slash;
int rc;
/* /*
* The argument supplied doesn't exist. Copy the path, and * The argument supplied doesn't exist. Copy the path, and
@ -126,23 +127,18 @@ main(int argc, char *argv[])
free(arg); free(arg);
if (dname == NULL || fname == NULL) if (dname == NULL || fname == NULL)
fail("strdup", 1); fail("strdup", 1);
dlen = strlen(dname);
flen = strlen(fname);
/* The directory portion of the path must exist */ /* The directory portion of the path must exist */
if ((ret = stat(dname, &sbuf)) != 0 || !(sbuf.st_mode & if ((ret = stat(dname, &sbuf)) != 0 || !(sbuf.st_mode &
S_IFDIR)) S_IFDIR))
usage(prog); usage(prog);
if ((fpath = (char *)malloc(dlen + 1 + flen + 1)) == NULL) rc = asprintf(&fpath, "%s/%s", dname, fname);
fail("malloc", 1);
(void) memset(fpath, '\0', dlen + 1 + flen + 1);
(void) strncpy(fpath, dname, dlen);
fpath[dlen] = '/';
(void) strncat(fpath, fname, flen);
free(dname); free(dname);
free(fname); free(fname);
if (rc == -1 || fpath == NULL)
fail("asprintf", 1);
} else if ((sbuf.st_mode & S_IFMT) == S_IFREG || } else if ((sbuf.st_mode & S_IFMT) == S_IFREG ||
(sbuf.st_mode & S_IFMT) == S_IFLNK || (sbuf.st_mode & S_IFMT) == S_IFLNK ||
(sbuf.st_mode & S_IFMT) == S_IFCHR || (sbuf.st_mode & S_IFMT) == S_IFCHR ||

View File

@ -137,8 +137,12 @@ mktree(char *pdir, int level)
static char * static char *
getfdname(char *pdir, char type, int level, int dir, int file) getfdname(char *pdir, char type, int level, int dir, int file)
{ {
(void) snprintf(fdname, sizeof (fdname), size_t size = sizeof (fdname);
"%s/%c-l%dd%df%d", pdir, type, level, dir, file); 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); return (fdname);
} }

View File

@ -367,8 +367,10 @@ create_files(void)
char *file = NULL; char *file = NULL;
struct timeval start, stop; struct timeval start, stop;
double seconds; double seconds;
size_t fsize;
file = malloc(PATH_MAX); fsize = PATH_MAX;
file = malloc(fsize);
if (file == NULL) { if (file == NULL) {
rc = ENOMEM; rc = ENOMEM;
ERROR("Error %d: malloc(%d) bytes for file name\n", rc, ERROR("Error %d: malloc(%d) bytes for file name\n", rc,
@ -379,7 +381,11 @@ create_files(void)
(void) gettimeofday(&start, NULL); (void) gettimeofday(&start, NULL);
for (i = 1; i <= files; i++) { 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)) if (nth && ((i % nth) == 0))
fprintf(stdout, "create: %s\n", file); fprintf(stdout, "create: %s\n", file);
@ -452,6 +458,7 @@ setxattrs(void)
char *file = NULL; char *file = NULL;
struct timeval start, stop; struct timeval start, stop;
double seconds; double seconds;
size_t fsize;
value = malloc(XATTR_SIZE_MAX); value = malloc(XATTR_SIZE_MAX);
if (value == NULL) { if (value == NULL) {
@ -461,7 +468,8 @@ setxattrs(void)
goto out; goto out;
} }
file = malloc(PATH_MAX); fsize = PATH_MAX;
file = malloc(fsize);
if (file == NULL) { if (file == NULL) {
rc = ENOMEM; rc = ENOMEM;
ERROR("Error %d: malloc(%d) bytes for file name\n", rc, ERROR("Error %d: malloc(%d) bytes for file name\n", rc,
@ -472,7 +480,11 @@ setxattrs(void)
(void) gettimeofday(&start, NULL); (void) gettimeofday(&start, NULL);
for (i = 1; i <= files; i++) { 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)) if (nth && ((i % nth) == 0))
fprintf(stdout, "setxattr: %s\n", file); fprintf(stdout, "setxattr: %s\n", file);
@ -523,6 +535,7 @@ getxattrs(void)
char *file = NULL; char *file = NULL;
struct timeval start, stop; struct timeval start, stop;
double seconds; double seconds;
size_t fsize;
verify_value = malloc(XATTR_SIZE_MAX); verify_value = malloc(XATTR_SIZE_MAX);
if (verify_value == NULL) { if (verify_value == NULL) {
@ -543,7 +556,9 @@ getxattrs(void)
verify_string = value_is_random ? "<random>" : verify_value; verify_string = value_is_random ? "<random>" : verify_value;
value_string = value_is_random ? "<random>" : value; value_string = value_is_random ? "<random>" : value;
file = malloc(PATH_MAX); fsize = PATH_MAX;
file = malloc(fsize);
if (file == NULL) { if (file == NULL) {
rc = ENOMEM; rc = ENOMEM;
ERROR("Error %d: malloc(%d) bytes for file name\n", rc, ERROR("Error %d: malloc(%d) bytes for file name\n", rc,
@ -554,7 +569,11 @@ getxattrs(void)
(void) gettimeofday(&start, NULL); (void) gettimeofday(&start, NULL);
for (i = 1; i <= files; i++) { 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)) if (nth && ((i % nth) == 0))
fprintf(stdout, "getxattr: %s\n", file); fprintf(stdout, "getxattr: %s\n", file);
@ -615,8 +634,10 @@ unlink_files(void)
char *file = NULL; char *file = NULL;
struct timeval start, stop; struct timeval start, stop;
double seconds; double seconds;
size_t fsize;
file = malloc(PATH_MAX); fsize = PATH_MAX;
file = malloc(fsize);
if (file == NULL) { if (file == NULL) {
rc = ENOMEM; rc = ENOMEM;
ERROR("Error %d: malloc(%d) bytes for file name\n", ERROR("Error %d: malloc(%d) bytes for file name\n",
@ -627,7 +648,11 @@ unlink_files(void)
(void) gettimeofday(&start, NULL); (void) gettimeofday(&start, NULL);
for (i = 1; i <= files; i++) { 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)) if (nth && ((i % nth) == 0))
fprintf(stdout, "unlink: %s\n", file); fprintf(stdout, "unlink: %s\n", file);