From 2db2979505554d7554258ec0196e1a45b5ca0115 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 16 Jan 2024 23:54:39 +0900 Subject: [PATCH] sd-network: modernize parse_operational_state_range() - rename 'out' -> 'ret', - introduce LINK_OPERSTATE_RANGE_INVALID, - constify LINK_OPERSTATE_RANGE_DEFAULT, - drop spurious const specifier for allocated string, - etc,. --- src/libsystemd/sd-network/network-util.c | 61 ++++++++++++------------ src/libsystemd/sd-network/network-util.h | 15 ++++-- 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/src/libsystemd/sd-network/network-util.c b/src/libsystemd/sd-network/network-util.c index 2059567ef89..25c6e44a779 100644 --- a/src/libsystemd/sd-network/network-util.c +++ b/src/libsystemd/sd-network/network-util.c @@ -90,49 +90,48 @@ static const char *const link_online_state_table[_LINK_ONLINE_STATE_MAX] = { DEFINE_STRING_TABLE_LOOKUP(link_online_state, LinkOnlineState); -int parse_operational_state_range(const char *str, LinkOperationalStateRange *out) { - LinkOperationalStateRange range = { _LINK_OPERSTATE_INVALID, _LINK_OPERSTATE_INVALID }; - _cleanup_free_ const char *min = NULL; +int parse_operational_state_range(const char *s, LinkOperationalStateRange *ret) { + LinkOperationalStateRange range = LINK_OPERSTATE_RANGE_INVALID; + _cleanup_free_ char *buf = NULL; const char *p; - assert(str); - assert(out); + assert(s); + assert(ret); + + /* allowed formats: "min", "min:", "min:max", ":max" */ + + if (isempty(s) || streq(s, ":")) + return -EINVAL; + + p = strchr(s, ':'); + if (!p || isempty(p + 1)) + range.max = LINK_OPERSTATE_ROUTABLE; + else { + range.max = link_operstate_from_string(p + 1); + if (range.max < 0) + return -EINVAL; + } - p = strchr(str, ':'); if (p) { - min = strndup(str, p - str); + buf = strndup(s, p - s); + if (!buf) + return -ENOMEM; - if (!isempty(p + 1)) { - range.max = link_operstate_from_string(p + 1); - if (range.max < 0) - return -EINVAL; - } - } else - min = strdup(str); + s = buf; + } - if (!min) - return -ENOMEM; - - if (!isempty(min)) { - range.min = link_operstate_from_string(min); + if (isempty(s)) + range.min = LINK_OPERSTATE_MISSING; + else { + range.min = link_operstate_from_string(s); if (range.min < 0) return -EINVAL; } - /* Fail on empty strings. */ - if (range.min == _LINK_OPERSTATE_INVALID && range.max == _LINK_OPERSTATE_INVALID) + if (!operational_state_range_is_valid(&range)) return -EINVAL; - if (range.min == _LINK_OPERSTATE_INVALID) - range.min = LINK_OPERSTATE_MISSING; - if (range.max == _LINK_OPERSTATE_INVALID) - range.max = LINK_OPERSTATE_ROUTABLE; - - if (range.min > range.max) - return -EINVAL; - - *out = range; - + *ret = range; return 0; } diff --git a/src/libsystemd/sd-network/network-util.h b/src/libsystemd/sd-network/network-util.h index 54cb7c49132..6fc6015902e 100644 --- a/src/libsystemd/sd-network/network-util.h +++ b/src/libsystemd/sd-network/network-util.h @@ -79,10 +79,19 @@ typedef struct LinkOperationalStateRange { LinkOperationalState max; } LinkOperationalStateRange; -#define LINK_OPERSTATE_RANGE_DEFAULT (LinkOperationalStateRange) { LINK_OPERSTATE_DEGRADED, \ - LINK_OPERSTATE_ROUTABLE } +#define LINK_OPERSTATE_RANGE_DEFAULT \ + (const LinkOperationalStateRange) { \ + .min = LINK_OPERSTATE_DEGRADED, \ + .max = LINK_OPERSTATE_ROUTABLE, \ + } -int parse_operational_state_range(const char *str, LinkOperationalStateRange *out); +#define LINK_OPERSTATE_RANGE_INVALID \ + (const LinkOperationalStateRange) { \ + .min = _LINK_OPERSTATE_INVALID, \ + .max = _LINK_OPERSTATE_INVALID, \ + } + +int parse_operational_state_range(const char *s, LinkOperationalStateRange *ret); int network_link_get_operational_state(int ifindex, LinkOperationalState *ret); static inline bool operational_state_is_valid(LinkOperationalState s) {