Fix userland resource leaks

Coverity caught these. With the exception of the file descriptor leak in
tests/zfs-tests/cmd/draid.c, they are all memory leaks.

Also, there is a piece of dead code in zfs_get_enclosure_sysfs_path().
We delete it as cleanup.

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Ryan Moeller <ryan@iXsystems.com>
Signed-off-by: Richard Yao <richard.yao@alumni.stonybrook.edu>
Closes #13921
This commit is contained in:
Richard Yao 2022-09-23 19:55:26 -04:00 committed by GitHub
parent 2a493a4c71
commit ebe1d03616
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 22 additions and 8 deletions

View File

@ -1137,6 +1137,8 @@ dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
} }
if (size == 0) { if (size == 0) {
if (data == NULL)
kmem_free(arr, oursize);
(void) printf("\t\t[]\n"); (void) printf("\t\t[]\n");
return; return;
} }
@ -7062,8 +7064,11 @@ import_checkpointed_state(char *target, nvlist_t *cfg, char **new_path)
freecfg = B_TRUE; freecfg = B_TRUE;
} }
if (asprintf(&bogus_name, "%s%s", poolname, BOGUS_SUFFIX) == -1) if (asprintf(&bogus_name, "%s%s", poolname, BOGUS_SUFFIX) == -1) {
if (target != poolname)
free(poolname);
return (NULL); return (NULL);
}
fnvlist_add_string(cfg, ZPOOL_CONFIG_POOL_NAME, bogus_name); fnvlist_add_string(cfg, ZPOOL_CONFIG_POOL_NAME, bogus_name);
error = spa_import(bogus_name, cfg, NULL, error = spa_import(bogus_name, cfg, NULL,
@ -7078,6 +7083,7 @@ import_checkpointed_state(char *target, nvlist_t *cfg, char **new_path)
if (new_path != NULL && path_start != NULL) { if (new_path != NULL && path_start != NULL) {
if (asprintf(new_path, "%s%s", bogus_name, path_start) == -1) { if (asprintf(new_path, "%s%s", bogus_name, path_start) == -1) {
free(bogus_name);
if (path_start != NULL) if (path_start != NULL)
free(poolname); free(poolname);
return (NULL); return (NULL);
@ -8198,8 +8204,7 @@ zdb_read_block(char *thing, spa_t *spa)
vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev); vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
if (vd == NULL) { if (vd == NULL) {
(void) printf("***Invalid vdev: %s\n", vdev); (void) printf("***Invalid vdev: %s\n", vdev);
free(dup); goto done;
return;
} else { } else {
if (vd->vdev_path) if (vd->vdev_path)
(void) fprintf(stderr, "Found vdev: %s\n", (void) fprintf(stderr, "Found vdev: %s\n",

View File

@ -7395,8 +7395,11 @@ unshare_unmount(int op, int argc, char **argv)
((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL)) ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
nomem(); nomem();
if ((mnttab = fopen(MNTTAB, "re")) == NULL) if ((mnttab = fopen(MNTTAB, "re")) == NULL) {
uu_avl_destroy(tree);
uu_avl_pool_destroy(pool);
return (ENOENT); return (ENOENT);
}
while (getmntent(mnttab, &entry) == 0) { while (getmntent(mnttab, &entry) == 0) {

View File

@ -295,6 +295,8 @@ zhack_do_feature_enable(int argc, char **argv)
feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT; feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT;
break; break;
case 'd': case 'd':
if (desc != NULL)
free(desc);
desc = strdup(optarg); desc = strdup(optarg);
break; break;
default: default:

View File

@ -1196,6 +1196,7 @@ zpool_do_remove(int argc, char **argv)
return (1); return (1);
if (stop && noop) { if (stop && noop) {
zpool_close(zhp);
(void) fprintf(stderr, gettext("stop request ignored\n")); (void) fprintf(stderr, gettext("stop request ignored\n"));
return (0); return (0);
} }

View File

@ -2007,6 +2007,7 @@ zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
goto error; goto error;
if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
changelist_free(cl);
return (zfs_standard_error(hdl, errno, errbuf)); return (zfs_standard_error(hdl, errno, errbuf));
} else { } else {

View File

@ -344,6 +344,8 @@ zfs_get_enclosure_sysfs_path(const char *dev_name)
if (strstr(ep->d_name, "enclosure_device") == NULL) if (strstr(ep->d_name, "enclosure_device") == NULL)
continue; continue;
if (tmp2 != NULL)
free(tmp2);
if (asprintf(&tmp2, "%s/%s", tmp1, ep->d_name) == -1) { if (asprintf(&tmp2, "%s/%s", tmp1, ep->d_name) == -1) {
tmp2 = NULL; tmp2 = NULL;
break; break;
@ -372,14 +374,13 @@ zfs_get_enclosure_sysfs_path(const char *dev_name)
if (tmp3 == NULL) if (tmp3 == NULL)
break; break;
if (path != NULL)
free(path);
if (asprintf(&path, "/sys/class/%s", tmp3) == -1) { if (asprintf(&path, "/sys/class/%s", tmp3) == -1) {
/* If asprintf() fails, 'path' is undefined */ /* If asprintf() fails, 'path' is undefined */
path = NULL; path = NULL;
break; break;
} }
if (path == NULL)
break;
} }
end: end:

View File

@ -233,13 +233,13 @@ drain_tree(zfs_btree_t *bt, char *why)
void *ret; void *ret;
u_longlong_t randval = random(); u_longlong_t randval = random();
node = malloc(sizeof (int_node_t));
if ((p = (uint64_t *)zfs_btree_find(bt, &randval, &bt_idx)) != if ((p = (uint64_t *)zfs_btree_find(bt, &randval, &bt_idx)) !=
NULL) { NULL) {
continue; continue;
} }
zfs_btree_add_idx(bt, &randval, &bt_idx); zfs_btree_add_idx(bt, &randval, &bt_idx);
node = malloc(sizeof (int_node_t));
node->data = randval; node->data = randval;
if ((ret = avl_find(&avl, node, &avl_idx)) != NULL) { if ((ret = avl_find(&avl, node, &avl_idx)) != NULL) {
snprintf(why, BUFSIZE, "Found in avl: %llu\n", randval); snprintf(why, BUFSIZE, "Found in avl: %llu\n", randval);

View File

@ -849,6 +849,7 @@ restart:
if (rc < 0) { if (rc < 0) {
printf("Unable to read /dev/urandom: %s\n:", printf("Unable to read /dev/urandom: %s\n:",
strerror(errno)); strerror(errno));
close(fd);
return (1); return (1);
} }
bytes_read += rc; bytes_read += rc;