aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPetr Machata <petrm@nvidia.com>2023-11-22 16:23:32 +0100
committerDavid Ahern <dsahern@kernel.org>2023-11-22 19:32:13 +0000
commitbd5226437a4c73404a0e50e419f1cd055b032a74 (patch)
treed3e23ecaf64fd268f616345d80289956fe53c7bf
parent2b8766663d3cbca96d07b87d74e9000b80c2a988 (diff)
downloadiproute2-bd5226437a4c73404a0e50e419f1cd055b032a74.tar.gz
lib: utils: Have parse_one_of() warn about prefix matches
The function parse_one_of() currently uses matches() for string comparison under the hood. Extending matches()-based parsers is tricky, because newly added matches might change the way strings are parsed, if the newly-added string shares a prefix with a string that is matched later in the code. Therefore in this patch, add a twist to parse_one_of() that partial prefix matches yield a warning. This will not disturb standard output or the overall behavior, but will make it obvious that the usage is discouraged and prompt users to update their scripts and habits. An example of output: # dcb ets set dev swp1 tc-tsa 0:s WARNING: 's' matches 'strict' by prefix. Matching by prefix is deprecated in this context, please use the full string. Signed-off-by: Petr Machata <petrm@nvidia.com> Signed-off-by: David Ahern <dsahern@kernel.org>
-rw-r--r--lib/utils.c19
1 files changed, 18 insertions, 1 deletions
diff --git a/lib/utils.c b/lib/utils.c
index 9142dc1d2..599e859ea 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -887,6 +887,23 @@ int matches(const char *prefix, const char *string)
return *prefix;
}
+static int matches_warn(const char *prefix, const char *string)
+{
+ int rc;
+
+ rc = matches(prefix, string);
+ if (rc)
+ return rc;
+
+ if (strlen(prefix) != strlen(string))
+ fprintf(stderr,
+ "WARNING: '%s' matches '%s' by prefix.\n"
+ "Matching by prefix is deprecated in this context, please use the full string.\n",
+ prefix, string);
+
+ return 0;
+}
+
int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits)
{
const __u32 *a1 = a->data;
@@ -1755,7 +1772,7 @@ __parse_one_of(const char *msg, const char *realval,
int parse_one_of(const char *msg, const char *realval, const char * const *list,
size_t len, int *p_err)
{
- return __parse_one_of(msg, realval, list, len, p_err, matches);
+ return __parse_one_of(msg, realval, list, len, p_err, matches_warn);
}
int parse_one_of_deprecated(const char *msg, const char *realval,