Skip to content

Commit

Permalink
nvpair: Constify string functions
Browse files Browse the repository at this point in the history
After addressing coverity complaints involving `nvpair_name()`, the
compiler started complaining about dropping const. This lead to a rabbit
hole where not only `nvpair_name()` needed to be constified, but also
`nvpair_value_string()`, `fnvpair_value_string()` and a few other static
functions, plus variable pointers throughout the code. The result became
a fairly big change, so it has been split out into its own patch.

Reviewed-by: Tino Reichardt <milky-zfs@mcmilk.de>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Richard Yao <richard.yao@alumni.stonybrook.edu>
Closes openzfs#14612
  • Loading branch information
ryao authored and lundman committed Mar 17, 2023
1 parent 205d2af commit f9d9a36
Show file tree
Hide file tree
Showing 71 changed files with 423 additions and 389 deletions.
2 changes: 1 addition & 1 deletion cmd/mount_zfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ parse_dataset(const char *target, char **dataset)

nvlist_t *cfg = NULL;
if (zpool_read_label(fd, &cfg, NULL) == 0) {
char *nm = NULL;
const char *nm = NULL;
if (!nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &nm))
strlcpy(*dataset, nm, PATH_MAX);
nvlist_free(cfg);
Expand Down
2 changes: 1 addition & 1 deletion cmd/zdb/zdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -4215,7 +4215,7 @@ collect_nvlist_stats(nvlist_t *nvl, zdb_nvl_stats_t *stats)
{
nvlist_t *list, **array;
nvpair_t *nvp = NULL;
char *name;
const char *name;
uint_t i, items;

stats->zns_list_count++;
Expand Down
6 changes: 3 additions & 3 deletions cmd/zed/agents/fmd_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ static void
zed_log_fault(nvlist_t *nvl, const char *uuid, const char *code)
{
nvlist_t *rsrc;
char *strval;
const char *strval;
uint64_t guid;
uint8_t byte;

Expand Down Expand Up @@ -389,7 +389,7 @@ zed_log_fault(nvlist_t *nvl, const char *uuid, const char *code)
static const char *
fmd_fault_mkcode(nvlist_t *fault)
{
char *class;
const char *class;
const char *code = "-";

/*
Expand Down Expand Up @@ -708,7 +708,7 @@ int
fmd_nvl_class_match(fmd_hdl_t *hdl, nvlist_t *nvl, const char *pattern)
{
(void) hdl;
char *class;
const char *class;

return (nvl != NULL &&
nvlist_lookup_string(nvl, FM_CLASS, &class) == 0 &&
Expand Down
6 changes: 3 additions & 3 deletions cmd/zed/agents/zfs_agents.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ typedef enum device_type {
typedef struct guid_search {
uint64_t gs_pool_guid;
uint64_t gs_vdev_guid;
char *gs_devid;
const char *gs_devid;
device_type_t gs_vdev_type;
uint64_t gs_vdev_expandtime; /* vdev expansion time */
} guid_search_t;
Expand All @@ -77,7 +77,7 @@ static boolean_t
zfs_agent_iter_vdev(zpool_handle_t *zhp, nvlist_t *nvl, void *arg)
{
guid_search_t *gsp = arg;
char *path = NULL;
const char *path = NULL;
uint_t c, children;
nvlist_t **child;
uint64_t vdev_guid;
Expand Down Expand Up @@ -211,7 +211,7 @@ zfs_agent_post_event(const char *class, const char *subclass, nvlist_t *nvl)
uint64_t pool_guid = 0, vdev_guid = 0;
guid_search_t search = { 0 };
device_type_t devtype = DEVICE_TYPE_PRIMARY;
char *devid = NULL;
const char *devid = NULL;

class = "resource.fs.zfs.removed";
subclass = "";
Expand Down
4 changes: 2 additions & 2 deletions cmd/zed/agents/zfs_diagnosis.c
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ zfs_fm_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
nvlist_t *detector;
boolean_t pool_found = B_FALSE;
boolean_t isresource;
char *type;
const char *type;

/*
* We subscribe to notifications for vdev or pool removal. In these
Expand Down Expand Up @@ -780,7 +780,7 @@ zfs_fm_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO_FAILURE)) ||
fmd_nvl_class_match(hdl, nvl,
ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_PROBE_FAILURE))) {
char *failmode = NULL;
const char *failmode = NULL;
boolean_t checkremove = B_FALSE;
uint32_t pri = 0;
int32_t flags = 0;
Expand Down
26 changes: 15 additions & 11 deletions cmd/zed/agents/zfs_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,15 @@ zfs_unavail_pool(zpool_handle_t *zhp, void *data)
static void
zfs_process_add(zpool_handle_t *zhp, nvlist_t *vdev, boolean_t labeled)
{
char *path;
const char *path;
vdev_state_t newstate;
nvlist_t *nvroot, *newvd;
pendingdev_t *device;
uint64_t wholedisk = 0ULL;
uint64_t offline = 0ULL, faulted = 0ULL;
uint64_t guid = 0ULL;
uint64_t is_spare = 0;
char *physpath = NULL, *new_devid = NULL, *enc_sysfs_path = NULL;
const char *physpath = NULL, *new_devid = NULL, *enc_sysfs_path = NULL;
char rawpath[PATH_MAX], fullpath[PATH_MAX];
char devpath[PATH_MAX];
int ret;
Expand Down Expand Up @@ -549,7 +549,7 @@ static void
zfs_iter_vdev(zpool_handle_t *zhp, nvlist_t *nvl, void *data)
{
dev_data_t *dp = data;
char *path = NULL;
const char *path = NULL;
uint_t c, children;
nvlist_t **child;
uint64_t guid = 0;
Expand Down Expand Up @@ -851,7 +851,7 @@ guid_iter(uint64_t pool_guid, uint64_t vdev_guid, const char *devid,
static int
zfs_deliver_add(nvlist_t *nvl)
{
char *devpath = NULL, *devid = NULL;
const char *devpath = NULL, *devid = NULL;
uint64_t pool_guid = 0, vdev_guid = 0;
boolean_t is_slice;

Expand Down Expand Up @@ -999,7 +999,8 @@ zfsdle_vdev_online(zpool_handle_t *zhp, void *data)
nvlist_t *tgt;
int error;

char *tmp_devname, devname[MAXPATHLEN] = "";
const char *tmp_devname;
char devname[MAXPATHLEN] = "";
uint64_t guid;

if (nvlist_lookup_uint64(udev_nvl, ZFS_EV_VDEV_GUID, &guid) == 0) {
Expand All @@ -1017,7 +1018,8 @@ zfsdle_vdev_online(zpool_handle_t *zhp, void *data)

if ((tgt = zpool_find_vdev_by_physpath(zhp, devname,
&avail_spare, &l2cache, NULL)) != NULL) {
char *path, fullpath[MAXPATHLEN];
const char *path;
char fullpath[MAXPATHLEN];
uint64_t wholedisk = 0;

error = nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &path);
Expand All @@ -1030,10 +1032,11 @@ zfsdle_vdev_online(zpool_handle_t *zhp, void *data)
&wholedisk);

if (wholedisk) {
char *tmp;
path = strrchr(path, '/');
if (path != NULL) {
path = zfs_strip_partition(path + 1);
if (path == NULL) {
tmp = zfs_strip_partition(path + 1);
if (tmp == NULL) {
zpool_close(zhp);
return (0);
}
Expand All @@ -1042,8 +1045,8 @@ zfsdle_vdev_online(zpool_handle_t *zhp, void *data)
return (0);
}

(void) strlcpy(fullpath, path, sizeof (fullpath));
free(path);
(void) strlcpy(fullpath, tmp, sizeof (fullpath));
free(tmp);

/*
* We need to reopen the pool associated with this
Expand Down Expand Up @@ -1148,7 +1151,8 @@ zfsdle_vdev_online(zpool_handle_t *zhp, void *data)
static int
zfs_deliver_dle(nvlist_t *nvl)
{
char *devname, name[MAXPATHLEN];
const char *devname;
char name[MAXPATHLEN];
uint64_t guid;

if (nvlist_lookup_uint64(nvl, ZFS_EV_VDEV_GUID, &guid) == 0) {
Expand Down
8 changes: 4 additions & 4 deletions cmd/zed/agents/zfs_retire.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ replace_with_spare(fmd_hdl_t *hdl, zpool_handle_t *zhp, nvlist_t *vdev)
*/
for (s = 0; s < nspares; s++) {
boolean_t rebuild = B_FALSE;
char *spare_name, *type;
const char *spare_name, *type;

if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
&spare_name) != 0)
Expand Down Expand Up @@ -377,9 +377,9 @@ zfs_retire_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl,
boolean_t is_repair;
boolean_t l2arc = B_FALSE;
boolean_t spare = B_FALSE;
char *scheme;
const char *scheme;
nvlist_t *vdev = NULL;
char *uuid;
const char *uuid;
int repair_done = 0;
boolean_t retire;
boolean_t is_disk;
Expand All @@ -401,7 +401,7 @@ zfs_retire_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl,
if (strcmp(class, "resource.fs.zfs.removed") == 0 ||
(strcmp(class, "resource.fs.zfs.statechange") == 0 &&
(state == VDEV_STATE_REMOVED || state == VDEV_STATE_FAULTED))) {
char *devtype;
const char *devtype;
char *devname;

if (nvlist_lookup_string(nvl, FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,
Expand Down
10 changes: 5 additions & 5 deletions cmd/zed/zed_disk_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct udev_monitor *g_mon;
static void
zed_udev_event(const char *class, const char *subclass, nvlist_t *nvl)
{
char *strval;
const char *strval;
uint64_t numval;

zed_log_msg(LOG_INFO, "zed_disk_event:");
Expand Down Expand Up @@ -178,7 +178,8 @@ static void *
zed_udev_monitor(void *arg)
{
struct udev_monitor *mon = arg;
char *tmp, *tmp2;
const char *tmp;
char *tmp2;

zed_log_msg(LOG_INFO, "Waiting for new udev disk events...");

Expand Down Expand Up @@ -336,7 +337,7 @@ zed_udev_monitor(void *arg)
if (strcmp(class, EC_DEV_STATUS) == 0 &&
udev_device_get_property_value(dev, "DM_UUID") &&
udev_device_get_property_value(dev, "MPATH_SBIN_PATH")) {
tmp = (char *)udev_device_get_devnode(dev);
tmp = udev_device_get_devnode(dev);
tmp2 = zfs_get_underlying_path(tmp);
if (tmp && tmp2 && (strcmp(tmp, tmp2) != 0)) {
/*
Expand All @@ -353,8 +354,7 @@ zed_udev_monitor(void *arg)
class = EC_DEV_ADD;
subclass = ESC_DISK;
} else {
tmp = (char *)
udev_device_get_property_value(dev,
tmp = udev_device_get_property_value(dev,
"DM_NR_VALID_PATHS");
/* treat as a multipath remove */
if (tmp != NULL && strcmp(tmp, "0") == 0) {
Expand Down
6 changes: 3 additions & 3 deletions cmd/zed/zed_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ _zed_event_add_string_array(uint64_t eid, zed_strings_t *zsp,
char buf[MAXBUF];
int buflen = sizeof (buf);
const char *name;
char **strp;
const char **strp;
uint_t nelem;
uint_t i;
char *p;
Expand Down Expand Up @@ -652,7 +652,7 @@ _zed_event_add_nvpair(uint64_t eid, zed_strings_t *zsp, nvpair_t *nvp)
uint16_t i16;
uint32_t i32;
uint64_t i64;
char *str;
const char *str;

assert(zsp != NULL);
assert(nvp != NULL);
Expand Down Expand Up @@ -935,7 +935,7 @@ zed_event_service(struct zed_conf *zcp)
uint64_t eid;
int64_t *etime;
uint_t nelem;
char *class;
const char *class;
const char *subclass;
int rv;

Expand Down
2 changes: 1 addition & 1 deletion cmd/zfs/zfs_iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ zfs_sort(const void *larg, const void *rarg, void *data)

for (psc = sc; psc != NULL; psc = psc->sc_next) {
char lbuf[ZFS_MAXPROPLEN], rbuf[ZFS_MAXPROPLEN];
char *lstr, *rstr;
const char *lstr, *rstr;
uint64_t lnum = 0, rnum = 0;
boolean_t lvalid, rvalid;
int ret = 0;
Expand Down
19 changes: 10 additions & 9 deletions cmd/zfs/zfs_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ zfs_do_create(int argc, char **argv)
int ret = 1;
nvlist_t *props;
uint64_t intval;
char *strval;
const char *strval;

if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
nomem();
Expand Down Expand Up @@ -1173,11 +1173,12 @@ zfs_do_create(int argc, char **argv)

if (volblocksize != ZVOL_DEFAULT_BLOCKSIZE &&
nvlist_lookup_string(props, prop, &strval) != 0) {
if (asprintf(&strval, "%llu",
char *tmp;
if (asprintf(&tmp, "%llu",
(u_longlong_t)volblocksize) == -1)
nomem();
nvlist_add_string(props, prop, strval);
free(strval);
nvlist_add_string(props, prop, tmp);
free(tmp);
}

/*
Expand Down Expand Up @@ -1252,7 +1253,7 @@ zfs_do_create(int argc, char **argv)
dryrun ? "would create %s\n" : "create %s\n", argv[0]);
while ((nvp = nvlist_next_nvpair(props, nvp)) != NULL) {
uint64_t uval;
char *sval;
const char *sval;

switch (nvpair_type(nvp)) {
case DATA_TYPE_UINT64:
Expand Down Expand Up @@ -2702,8 +2703,8 @@ us_compare(const void *larg, const void *rarg, void *unused)
boolean_t lvb, rvb;

for (; sortcol != NULL; sortcol = sortcol->sc_next) {
char *lvstr = (char *)"";
char *rvstr = (char *)"";
const char *lvstr = "";
const char *rvstr = "";
uint32_t lv32 = 0;
uint32_t rv32 = 0;
uint64_t lv64 = 0;
Expand Down Expand Up @@ -6499,7 +6500,7 @@ print_holds(boolean_t scripted, int nwidth, int tagwidth, nvlist_t *nvl,
}

while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
char *zname = nvpair_name(nvp);
const char *zname = nvpair_name(nvp);
nvlist_t *nvl2;
nvpair_t *nvp2 = NULL;
(void) nvpair_value_nvlist(nvp, &nvl2);
Expand Down Expand Up @@ -8119,7 +8120,7 @@ zfs_do_channel_program(int argc, char **argv)
const char *msg = gettext("Channel program execution failed");
uint64_t instructions = 0;
if (outnvl != NULL && nvlist_exists(outnvl, ZCP_RET_ERROR)) {
char *es = NULL;
const char *es = NULL;
(void) nvlist_lookup_string(outnvl,
ZCP_RET_ERROR, &es);
if (es == NULL)
Expand Down
4 changes: 2 additions & 2 deletions cmd/zpool/zpool_iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -564,9 +564,9 @@ for_each_vdev_run_cb(void *zhp_data, nvlist_t *nv, void *cb_vcdl)
{
vdev_cmd_data_list_t *vcdl = cb_vcdl;
vdev_cmd_data_t *data;
char *path = NULL;
const char *path = NULL;
char *vname = NULL;
char *vdev_enc_sysfs_path = NULL;
const char *vdev_enc_sysfs_path = NULL;
int i, match = 0;
zpool_handle_t *zhp = zhp_data;

Expand Down
Loading

0 comments on commit f9d9a36

Please sign in to comment.