From d1807f168edd09ca26a5a0c6b570686b982808ad Mon Sep 17 00:00:00 2001 From: Richard Yao Date: Sat, 11 Mar 2023 13:39:24 -0500 Subject: nvpair: Constify string functions 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 Reviewed-by: Brian Behlendorf Signed-off-by: Richard Yao Closes #14612 --- lib/libnvpair/libnvpair.abi | 74 +++++++++++++++++++++++------------------- lib/libnvpair/libnvpair.c | 41 +++++++++++------------ lib/libnvpair/libnvpair_json.c | 4 +-- 3 files changed, 64 insertions(+), 55 deletions(-) (limited to 'lib/libnvpair') diff --git a/lib/libnvpair/libnvpair.abi b/lib/libnvpair/libnvpair.abi index 5900a6551..ef92f3e9b 100644 --- a/lib/libnvpair/libnvpair.abi +++ b/lib/libnvpair/libnvpair.abi @@ -243,6 +243,9 @@ + + + @@ -321,6 +324,9 @@ + + + @@ -698,7 +704,7 @@ - + @@ -802,7 +808,7 @@ - + @@ -858,11 +864,11 @@ - - + + @@ -871,8 +877,8 @@ - - + + @@ -898,8 +904,8 @@ - - + + @@ -966,7 +972,7 @@ - + @@ -1027,8 +1033,8 @@ - - + + @@ -1098,7 +1104,7 @@ - + @@ -1280,7 +1286,7 @@ - + @@ -1358,7 +1364,7 @@ - + @@ -1393,16 +1399,16 @@ - + - + - - + + @@ -1414,20 +1420,20 @@ - + - + - + - + @@ -1642,20 +1648,20 @@ - + - + - + - + @@ -1930,8 +1936,8 @@ - - + + @@ -2327,9 +2333,9 @@ - + - + @@ -2466,7 +2472,9 @@ + + @@ -2925,9 +2933,9 @@ - + - + @@ -3043,7 +3051,7 @@ - + diff --git a/lib/libnvpair/libnvpair.c b/lib/libnvpair/libnvpair.c index a75e73167..f19ee707a 100644 --- a/lib/libnvpair/libnvpair.c +++ b/lib/libnvpair/libnvpair.c @@ -70,7 +70,7 @@ struct nvlist_printops { DEFINEOP(print_int64, int64_t); DEFINEOP(print_uint64, uint64_t); DEFINEOP(print_double, double); - DEFINEOP(print_string, char *); + DEFINEOP(print_string, const char *); DEFINEOP(print_hrtime, hrtime_t); DEFINEOP(print_nvlist, nvlist_t *); DEFINEARROP(print_boolean_array, boolean_t *); @@ -83,7 +83,7 @@ struct nvlist_printops { DEFINEARROP(print_uint32_array, uint32_t *); DEFINEARROP(print_int64_array, int64_t *); DEFINEARROP(print_uint64_array, uint64_t *); - DEFINEARROP(print_string_array, char **); + DEFINEARROP(print_string_array, const char **); DEFINEARROP(print_nvlist_array, nvlist_t **); }; @@ -222,7 +222,7 @@ NVLIST_PRTFUNC(uint32, uint32_t, uint32_t, "0x%x") NVLIST_PRTFUNC(int64, int64_t, longlong_t, "%lld") NVLIST_PRTFUNC(uint64, uint64_t, u_longlong_t, "0x%llx") NVLIST_PRTFUNC(double, double, double, "0x%f") -NVLIST_PRTFUNC(string, char *, char *, "%s") +NVLIST_PRTFUNC(string, const char *, const char *, "%s") NVLIST_PRTFUNC(hrtime, hrtime_t, hrtime_t, "0x%llx") #if defined(__GNUC__) && !defined(__clang__) && \ defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW) @@ -266,7 +266,7 @@ NVLIST_ARRPRTFUNC(int32_array, int32_t, int32_t, "%d") NVLIST_ARRPRTFUNC(uint32_array, uint32_t, uint32_t, "0x%x") NVLIST_ARRPRTFUNC(int64_array, int64_t, longlong_t, "%lld") NVLIST_ARRPRTFUNC(uint64_array, uint64_t, u_longlong_t, "0x%llx") -NVLIST_ARRPRTFUNC(string_array, char *, char *, "%s") +NVLIST_ARRPRTFUNC(string_array, const char *, const char *, "%s") static int nvprint_nvlist(nvlist_prtctl_t pctl, void *private, @@ -398,13 +398,13 @@ nvlist_prtctl_dofmt(nvlist_prtctl_t pctl, enum nvlist_prtctl_fmt which, ...) { FILE *fp = pctl->nvprt_fp; va_list ap; - char *name; + const char *name; va_start(ap, which); switch (which) { case NVLIST_FMT_MEMBER_NAME: - name = va_arg(ap, char *); + name = va_arg(ap, const char *); (void) fprintf(fp, pctl->nvprt_nmfmt, name); break; @@ -453,7 +453,7 @@ NVLIST_PRINTCTL_REPLACE(uint32, uint32_t) NVLIST_PRINTCTL_REPLACE(int64, int64_t) NVLIST_PRINTCTL_REPLACE(uint64, uint64_t) NVLIST_PRINTCTL_REPLACE(double, double) -NVLIST_PRINTCTL_REPLACE(string, char *) +NVLIST_PRINTCTL_REPLACE(string, const char *) NVLIST_PRINTCTL_REPLACE(hrtime, hrtime_t) NVLIST_PRINTCTL_REPLACE(nvlist, nvlist_t *) @@ -477,7 +477,7 @@ NVLIST_PRINTCTL_AREPLACE(int32_array, int32_t *) NVLIST_PRINTCTL_AREPLACE(uint32_array, uint32_t *) NVLIST_PRINTCTL_AREPLACE(int64_array, int64_t *) NVLIST_PRINTCTL_AREPLACE(uint64_array, uint64_t *) -NVLIST_PRINTCTL_AREPLACE(string_array, char **) +NVLIST_PRINTCTL_AREPLACE(string_array, const char **) NVLIST_PRINTCTL_AREPLACE(nvlist_array, nvlist_t **) /* @@ -580,7 +580,7 @@ static void nvlist_print_with_indent(nvlist_t *nvl, nvlist_prtctl_t pctl) { FILE *fp = pctl->nvprt_fp; - char *name; + const char *name; uint_t nelem; nvpair_t *nvp; @@ -670,7 +670,7 @@ nvlist_print_with_indent(nvlist_t *nvl, nvlist_prtctl_t pctl) break; } case DATA_TYPE_STRING: { - char *val; + const char *val; (void) nvpair_value_string(nvp, &val); RENDER(pctl, string, nvl, name, val); break; @@ -736,7 +736,7 @@ nvlist_print_with_indent(nvlist_t *nvl, nvlist_prtctl_t pctl) break; } case DATA_TYPE_STRING_ARRAY: { - char **val; + const char **val; (void) nvpair_value_string_array(nvp, &val, &nelem); ARENDER(pctl, string_array, nvl, name, val, nelem); break; @@ -866,7 +866,7 @@ dump_nvlist(nvlist_t *list, int indent) break; case DATA_TYPE_STRING: - NVP(elem, string, char *, char *, "'%s'"); + NVP(elem, string, const char *, const char *, "'%s'"); break; case DATA_TYPE_BYTE_ARRAY: @@ -907,7 +907,8 @@ dump_nvlist(nvlist_t *list, int indent) break; case DATA_TYPE_STRING_ARRAY: - NVPA(elem, string_array, char *, char *, "'%s'"); + NVPA(elem, string_array, const char *, const char *, + "'%s'"); break; case DATA_TYPE_NVLIST: @@ -961,11 +962,11 @@ dump_nvlist(nvlist_t *list, int indent) */ int nvpair_value_match_regex(nvpair_t *nvp, int ai, - char *value, regex_t *value_regex, char **ep) + const char *value, regex_t *value_regex, const char **ep) { - char *evalue; - uint_t a_len; - int sr; + const char *evalue; + uint_t a_len; + int sr; if (ep) *ep = NULL; @@ -993,7 +994,7 @@ nvpair_value_match_regex(nvpair_t *nvp, int ai, sr = EOF; switch (nvpair_type(nvp)) { case DATA_TYPE_STRING: { - char *val; + const char *val; /* check string value for match */ if (nvpair_value_string(nvp, &val) == 0) { @@ -1009,7 +1010,7 @@ nvpair_value_match_regex(nvpair_t *nvp, int ai, break; } case DATA_TYPE_STRING_ARRAY: { - char **val_array; + const char **val_array; /* check indexed string value of array for match */ if ((nvpair_value_string_array(nvp, &val_array, &a_len) == 0) && @@ -1285,7 +1286,7 @@ nvpair_value_match_regex(nvpair_t *nvp, int ai, } int -nvpair_value_match(nvpair_t *nvp, int ai, char *value, char **ep) +nvpair_value_match(nvpair_t *nvp, int ai, const char *value, const char **ep) { return (nvpair_value_match_regex(nvp, ai, value, NULL, ep)); } diff --git a/lib/libnvpair/libnvpair_json.c b/lib/libnvpair/libnvpair_json.c index 19acea8f5..b8029dd4d 100644 --- a/lib/libnvpair/libnvpair_json.c +++ b/lib/libnvpair/libnvpair_json.c @@ -134,7 +134,7 @@ nvlist_print_json(FILE *fp, nvlist_t *nvl) switch (type) { case DATA_TYPE_STRING: { - char *string = fnvpair_value_string(curr); + const char *string = fnvpair_value_string(curr); if (nvlist_print_json_string(fp, string) == -1) return (-1); break; @@ -220,7 +220,7 @@ nvlist_print_json(FILE *fp, nvlist_t *nvl) } case DATA_TYPE_STRING_ARRAY: { - char **val; + const char **val; uint_t valsz, i; VERIFY0(nvpair_value_string_array(curr, &val, &valsz)); FPRINTF(fp, "["); -- cgit v1.2.3