Skip to content

Commit

Permalink
libzfs: don't NULL-check infallible allocations
Browse files Browse the repository at this point in the history
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Closes #13229
  • Loading branch information
nabijaczleweli authored and behlendorf committed Mar 30, 2022
1 parent bc3f12b commit 18dbf5c
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 262 deletions.
25 changes: 4 additions & 21 deletions lib/libzfs/libzfs_changelist.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,12 +447,7 @@ changelist_add_mounted(zfs_handle_t *zhp, void *data)

ASSERT3U(clp->cl_prop, ==, ZFS_PROP_MOUNTPOINT);

if ((cn = zfs_alloc(zfs_get_handle(zhp),
sizeof (prop_changenode_t))) == NULL) {
zfs_close(zhp);
return (ENOMEM);
}

cn = zfs_alloc(zfs_get_handle(zhp), sizeof (prop_changenode_t));
cn->cn_handle = zhp;
cn->cn_mounted = zfs_is_mounted(zhp, NULL);
ASSERT3U(cn->cn_mounted, ==, B_TRUE);
Expand Down Expand Up @@ -522,12 +517,7 @@ change_one(zfs_handle_t *zhp, void *data)
(clp->cl_shareprop != ZPROP_INVAL &&
(share_sourcetype == ZPROP_SRC_DEFAULT ||
share_sourcetype == ZPROP_SRC_INHERITED))) {
if ((cn = zfs_alloc(zfs_get_handle(zhp),
sizeof (prop_changenode_t))) == NULL) {
ret = -1;
goto out;
}

cn = zfs_alloc(zfs_get_handle(zhp), sizeof (prop_changenode_t));
cn->cn_handle = zhp;
cn->cn_mounted = (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) ||
zfs_is_mounted(zhp, NULL);
Expand Down Expand Up @@ -630,8 +620,7 @@ changelist_gather(zfs_handle_t *zhp, zfs_prop_t prop, int gather_flags,
char property[ZFS_MAXPROPLEN];
boolean_t legacy = B_FALSE;

if ((clp = zfs_alloc(zhp->zfs_hdl, sizeof (prop_changelist_t))) == NULL)
return (NULL);
clp = zfs_alloc(zhp->zfs_hdl, sizeof (prop_changelist_t));

/*
* For mountpoint-related tasks, we want to sort everything by
Expand Down Expand Up @@ -744,13 +733,7 @@ changelist_gather(zfs_handle_t *zhp, zfs_prop_t prop, int gather_flags,
* Always add ourself to the list. We add ourselves to the end so that
* we're the last to be unmounted.
*/
if ((cn = zfs_alloc(zhp->zfs_hdl,
sizeof (prop_changenode_t))) == NULL) {
zfs_close(temp);
changelist_free(clp);
return (NULL);
}

cn = zfs_alloc(zhp->zfs_hdl, sizeof (prop_changenode_t));
cn->cn_handle = temp;
cn->cn_mounted = (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) ||
zfs_is_mounted(temp, NULL);
Expand Down
34 changes: 8 additions & 26 deletions lib/libzfs/libzfs_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ namespace_reload(libzfs_handle_t *hdl)
return (no_memory(hdl));
}

if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
return (-1);
zcmd_alloc_dst_nvlist(hdl, &zc, 0);

for (;;) {
zc.zc_cookie = hdl->libzfs_ns_gen;
Expand All @@ -141,10 +140,7 @@ namespace_reload(libzfs_handle_t *hdl)
return (0);

case ENOMEM:
if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
zcmd_free_nvlists(&zc);
return (-1);
}
zcmd_expand_dst_nvlist(hdl, &zc);
break;

default:
Expand Down Expand Up @@ -181,18 +177,8 @@ namespace_reload(libzfs_handle_t *hdl)
nvlist_t *child;
uu_avl_index_t where;

if ((cn = zfs_alloc(hdl, sizeof (config_node_t))) == NULL) {
nvlist_free(config);
return (-1);
}

if ((cn->cn_name = zfs_strdup(hdl,
nvpair_name(elem))) == NULL) {
free(cn);
nvlist_free(config);
return (-1);
}

cn = zfs_alloc(hdl, sizeof (config_node_t));
cn->cn_name = zfs_strdup(hdl, nvpair_name(elem));
child = fnvpair_value_nvlist(elem);
if (nvlist_dup(child, &cn->cn_config, 0) != 0) {
free(cn->cn_name);
Expand Down Expand Up @@ -273,8 +259,7 @@ zpool_refresh_stats(zpool_handle_t *zhp, boolean_t *missing)
if (zhp->zpool_config_size == 0)
zhp->zpool_config_size = 1 << 16;

if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size) != 0)
return (-1);
zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size);

for (;;) {
if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_STATS,
Expand All @@ -286,12 +271,9 @@ zpool_refresh_stats(zpool_handle_t *zhp, boolean_t *missing)
break;
}

if (errno == ENOMEM) {
if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
zcmd_free_nvlists(&zc);
return (-1);
}
} else {
if (errno == ENOMEM)
zcmd_expand_dst_nvlist(hdl, &zc);
else {
zcmd_free_nvlists(&zc);
if (errno == ENOENT || errno == EINVAL)
*missing = B_TRUE;
Expand Down
2 changes: 0 additions & 2 deletions lib/libzfs/libzfs_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -783,8 +783,6 @@ derive_key(libzfs_handle_t *hdl, zfs_keyformat_t format, uint64_t iters,
*key_out = NULL;

key = zfs_alloc(hdl, WRAPPING_KEY_LEN);
if (!key)
return (ENOMEM);

switch (format) {
case ZFS_KEYFORMAT_RAW:
Expand Down
67 changes: 21 additions & 46 deletions lib/libzfs/libzfs_dataset.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,10 @@ get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));

while (zfs_ioctl(hdl, ZFS_IOC_OBJSET_STATS, zc) != 0) {
if (errno == ENOMEM) {
if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
return (-1);
}
} else {
if (errno == ENOMEM)
zcmd_expand_dst_nvlist(hdl, zc);
else
return (-1);
}
}
return (0);
}
Expand All @@ -353,17 +350,14 @@ get_recvd_props_ioctl(zfs_handle_t *zhp)
zfs_cmd_t zc = {"\0"};
int err;

if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
return (-1);
zcmd_alloc_dst_nvlist(hdl, &zc, 0);

(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));

while (zfs_ioctl(hdl, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
if (errno == ENOMEM) {
if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
return (-1);
}
} else {
if (errno == ENOMEM)
zcmd_expand_dst_nvlist(hdl, &zc);
else {
zcmd_free_nvlists(&zc);
return (-1);
}
Expand Down Expand Up @@ -415,8 +409,8 @@ get_stats(zfs_handle_t *zhp)
int rc = 0;
zfs_cmd_t zc = {"\0"};

if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
return (-1);
zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0);

if (get_stats_ioctl(zhp, &zc) != 0)
rc = -1;
else if (put_stats_zhdl(zhp, &zc) != 0)
Expand Down Expand Up @@ -489,10 +483,8 @@ make_dataset_handle(libzfs_handle_t *hdl, const char *path)

zhp->zfs_hdl = hdl;
(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
free(zhp);
return (NULL);
}
zcmd_alloc_dst_nvlist(hdl, &zc, 0);

if (get_stats_ioctl(zhp, &zc) == -1) {
zcmd_free_nvlists(&zc);
free(zhp);
Expand Down Expand Up @@ -1847,9 +1839,8 @@ zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
*/
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));

if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
(ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
goto error;
zcmd_write_src_nvlist(hdl, &zc, nvl);
zcmd_alloc_dst_nvlist(hdl, &zc, 0);

ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);

Expand Down Expand Up @@ -1885,8 +1876,7 @@ zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
zfs_prop_to_name(ZFS_PROP_VOLSIZE),
old_volsize) != 0)
goto error;
if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
goto error;
zcmd_write_src_nvlist(hdl, &zc, nvl);
(void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
}
} else {
Expand Down Expand Up @@ -2195,12 +2185,9 @@ get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
libzfs_handle_t *hdl = zhp->zfs_hdl;
struct mnttab entry;

if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)
zhp->zfs_mntopts = zfs_strdup(hdl,
entry.mnt_mntopts);
if (zhp->zfs_mntopts == NULL)
return (-1);
}

zhp->zfs_mntcheck = B_TRUE;
}
Expand Down Expand Up @@ -2267,8 +2254,8 @@ get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
case ZFS_PROP_NORMALIZE:
case ZFS_PROP_UTF8ONLY:
case ZFS_PROP_CASE:
if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
return (-1);
zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0);

(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
zcmd_free_nvlists(&zc);
Expand Down Expand Up @@ -4505,10 +4492,6 @@ zfs_rename(zfs_handle_t *zhp, const char *target, renameflags_t flags)
}
if (flags.recursive) {
char *parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
if (parentname == NULL) {
ret = -1;
goto error;
}
delim = strchr(parentname, '@');
*delim = '\0';
zfs_handle_t *zhrp = zfs_open(zhp->zfs_hdl, parentname,
Expand Down Expand Up @@ -4670,14 +4653,9 @@ zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
}

if (*last == NULL) {
if ((entry = zfs_alloc(hdl,
sizeof (zprop_list_t))) == NULL ||
((entry->pl_user_prop = zfs_strdup(hdl,
nvpair_name(elem)))) == NULL) {
free(entry);
return (-1);
}

entry = zfs_alloc(hdl, sizeof (zprop_list_t));
entry->pl_user_prop =
zfs_strdup(hdl, nvpair_name(elem));
entry->pl_prop = ZPROP_INVAL;
entry->pl_width = strlen(nvpair_name(elem));
entry->pl_all = B_TRUE;
Expand Down Expand Up @@ -4790,10 +4768,7 @@ zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
(void) no_memory(hdl);
return (-1);
}
if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
nvlist_free(nvlist);
return (-1);
}
zcmd_write_src_nvlist(hdl, &zc, nvlist);
break;
case ZFS_SMB_ACL_PURGE:
break;
Expand Down
5 changes: 2 additions & 3 deletions lib/libzfs/libzfs_diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -602,11 +602,10 @@ get_snapshot_names(differ_info_t *di, const char *fromsnap,

di->isclone = B_TRUE;
di->fromsnap = zfs_strdup(hdl, fromsnap);
if (tsnlen) {
if (tsnlen)
di->tosnap = zfs_strdup(hdl, tosnap);
} else {
else
return (make_temp_snapshot(di));
}
} else {
int dslen = fdslen ? fdslen : tdslen;

Expand Down
8 changes: 4 additions & 4 deletions lib/libzfs/libzfs_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,10 @@ extern int zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp,

typedef struct prop_changelist prop_changelist_t;

extern int zcmd_alloc_dst_nvlist(libzfs_handle_t *, zfs_cmd_t *, size_t);
extern int zcmd_write_src_nvlist(libzfs_handle_t *, zfs_cmd_t *, nvlist_t *);
extern int zcmd_write_conf_nvlist(libzfs_handle_t *, zfs_cmd_t *, nvlist_t *);
extern int zcmd_expand_dst_nvlist(libzfs_handle_t *, zfs_cmd_t *);
extern void zcmd_alloc_dst_nvlist(libzfs_handle_t *, zfs_cmd_t *, size_t);
extern void zcmd_write_src_nvlist(libzfs_handle_t *, zfs_cmd_t *, nvlist_t *);
extern void zcmd_write_conf_nvlist(libzfs_handle_t *, zfs_cmd_t *, nvlist_t *);
extern void zcmd_expand_dst_nvlist(libzfs_handle_t *, zfs_cmd_t *);
extern int zcmd_read_dst_nvlist(libzfs_handle_t *, zfs_cmd_t *, nvlist_t **);
extern void zcmd_free_nvlists(zfs_cmd_t *);

Expand Down
23 changes: 5 additions & 18 deletions lib/libzfs/libzfs_import.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,15 @@ refresh_config(libzfs_handle_t *hdl, nvlist_t *config)
zfs_cmd_t zc = {"\0"};
int err, dstbuf_size;

if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0)
return (NULL);
zcmd_write_conf_nvlist(hdl, &zc, config);

dstbuf_size = MAX(CONFIG_BUF_MINSIZE, zc.zc_nvlist_conf_size * 32);

if (zcmd_alloc_dst_nvlist(hdl, &zc, dstbuf_size) != 0) {
zcmd_free_nvlists(&zc);
return (NULL);
}
zcmd_alloc_dst_nvlist(hdl, &zc, dstbuf_size);

while ((err = zfs_ioctl(hdl, ZFS_IOC_POOL_TRYIMPORT,
&zc)) != 0 && errno == ENOMEM) {
if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
zcmd_free_nvlists(&zc);
return (NULL);
}
}
&zc)) != 0 && errno == ENOMEM)
zcmd_expand_dst_nvlist(hdl, &zc);

if (err) {
zcmd_free_nvlists(&zc);
Expand Down Expand Up @@ -442,12 +434,7 @@ zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr,


if (ret) {
if ((*namestr = zfs_strdup(hdl, name)) == NULL) {
if (cb.cb_zhp)
zpool_close(cb.cb_zhp);
nvlist_free(config);
return (-1);
}
*namestr = zfs_strdup(hdl, name);
*state = (pool_state_t)stateval;
}

Expand Down
19 changes: 5 additions & 14 deletions lib/libzfs/libzfs_iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
switch (errno) {
case ENOMEM:
/* expand nvlist memory and try again */
if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
zcmd_free_nvlists(zc);
return (-1);
}
zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc);
zc->zc_cookie = orig_cookie;
goto top;
/*
Expand Down Expand Up @@ -113,8 +110,7 @@ zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
return (0);

if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
return (-1);
zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0);

while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
&zc)) == 0) {
Expand Down Expand Up @@ -154,8 +150,7 @@ zfs_iter_snapshots(zfs_handle_t *zhp, boolean_t simple, zfs_iter_f func,

zc.zc_simple = simple;

if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
return (-1);
zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0);

if (min_txg != 0) {
range_nvl = fnvlist_alloc();
Expand All @@ -167,12 +162,8 @@ zfs_iter_snapshots(zfs_handle_t *zhp, boolean_t simple, zfs_iter_f func,
fnvlist_add_uint64(range_nvl, SNAP_ITER_MAX_TXG, max_txg);
}

if (range_nvl != NULL &&
zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, range_nvl) != 0) {
zcmd_free_nvlists(&zc);
fnvlist_free(range_nvl);
return (-1);
}
if (range_nvl != NULL)
zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, range_nvl);

while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
&zc)) == 0) {
Expand Down
Loading

0 comments on commit 18dbf5c

Please sign in to comment.