From bc56542924619f49773c44ac21d62124dac7ed76 Mon Sep 17 00:00:00 2001 Message-ID: In-Reply-To: <7f2e9f746e820e43bafc4ecee18c33a7b6bf5d48.1769541298.git.aclaudi@redhat.com> References: <7f2e9f746e820e43bafc4ecee18c33a7b6bf5d48.1769541298.git.aclaudi@redhat.com> From: Andrea Claudi Date: Tue, 27 Jan 2026 19:59:11 +0100 Subject: [PATCH] lib: Add str_to_bool helper function JIRA: https://issues.redhat.com/browse/RHEL-131661 Upstream Status: iproute2.git commit 42f2f219c618f commit 42f2f219c618f0c1bf07e536f5cdcc27cb0d6a4b Author: Petr Oros Date: Tue Nov 18 15:10:30 2025 +0100 lib: Add str_to_bool helper function Add str_to_bool() helper function to lib/utils.c that uses parse_one_of() to parse boolean values. Update devlink to use this common implementation. Signed-off-by: Petr Oros Signed-off-by: David Ahern Signed-off-by: Andrea Claudi --- devlink/devlink.c | 22 +++------------------- include/utils.h | 1 + lib/utils.c | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/devlink/devlink.c b/devlink/devlink.c index 171b8532..89b89d9c 100644 --- a/devlink/devlink.c +++ b/devlink/devlink.c @@ -1038,22 +1038,6 @@ static int ifname_map_rev_lookup(struct dl *dl, const char *bus_name, return -ENOENT; } -static int strtobool(const char *str, bool *p_val) -{ - bool val; - - if (!strcmp(str, "true") || !strcmp(str, "1") || - !strcmp(str, "enable")) - val = true; - else if (!strcmp(str, "false") || !strcmp(str, "0") || - !strcmp(str, "disable")) - val = false; - else - return -EINVAL; - *p_val = val; - return 0; -} - static int ident_str_validate(char *str, unsigned int expected) { if (!str) @@ -1356,7 +1340,7 @@ static int dl_argv_bool(struct dl *dl, bool *p_val) return -EINVAL; } - err = strtobool(str, p_val); + err = str_to_bool(str, p_val); if (err) { pr_err("\"%s\" is not a valid boolean value\n", str); return err; @@ -3821,7 +3805,7 @@ static int cmd_dev_param_set(struct dl *dl) mnl_attr_put_u32(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, val_u32); break; case MNL_TYPE_FLAG: - err = strtobool(dl->opts.param_value, &val_bool); + err = str_to_bool(dl->opts.param_value, &val_bool); if (err) goto err_param_value_parse; if (val_bool == ctx.value.vbool) @@ -5393,7 +5377,7 @@ static int cmd_port_param_set(struct dl *dl) mnl_attr_put_u32(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, val_u32); break; case MNL_TYPE_FLAG: - err = strtobool(dl->opts.param_value, &val_bool); + err = str_to_bool(dl->opts.param_value, &val_bool); if (err) goto err_param_value_parse; if (val_bool == ctx.value.vbool) diff --git a/include/utils.h b/include/utils.h index 91e6e31f..d8e8d70e 100644 --- a/include/utils.h +++ b/include/utils.h @@ -350,6 +350,7 @@ int parse_one_of_deprecated(const char *msg, const char *realval, const char * const *list, size_t len, int *p_err); bool parse_on_off(const char *msg, const char *realval, int *p_err); +int str_to_bool(const char *str, bool *p_val); int parse_mapping_num_all(__u32 *keyp, const char *key); int parse_mapping_gen(int *argcp, char ***argvp, diff --git a/lib/utils.c b/lib/utils.c index dd242d4d..0719281a 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -1820,6 +1820,23 @@ bool parse_on_off(const char *msg, const char *realval, int *p_err) ARRAY_SIZE(values_on_off), p_err, strcmp); } +int str_to_bool(const char *str, bool *p_val) +{ + static const char * const values[] = { + "false", "true", + "0", "1", + "disable", "enable" + }; + int err, index; + + index = parse_one_of(NULL, str, values, ARRAY_SIZE(values), &err); + if (err) + return err; + + *p_val = index & 1; + return 0; +} + int parse_mapping_gen(int *argcp, char ***argvp, int (*key_cb)(__u32 *keyp, const char *key), int (*mapping_cb)(__u32 key, char *value, void *data), -- 2.52.0