From 38844e6c5ead114f3ce3a7ea165c4db71eed19a4 Mon Sep 17 00:00:00 2001 From: Francesco Giudici Date: Mon, 5 Feb 2018 19:00:08 +0100 Subject: [PATCH] client: fix nmc_string_is_valid ambiguous detection when input matched the heading of two allowed values the match was reported as ambiguous without checking if there was a perfect match following: fixed. Example of a failing input: const char **allowed = [ "ipv4, ipv6, ip" ]; const char *input = "ip"; "ip" was detected as ambiguous. --- clients/common/nm-client-utils.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/clients/common/nm-client-utils.c b/clients/common/nm-client-utils.c index 194271a86c..c312f8cdf7 100644 --- a/clients/common/nm-client-utils.c +++ b/clients/common/nm-client-utils.c @@ -134,7 +134,7 @@ nmc_string_is_valid (const char *input, const char **allowed, GError **error) { const char **p; size_t input_ln, p_len; - gboolean prev_match = FALSE; + gboolean prev_match = FALSE, ambiguous = FALSE; const char *ret = NULL; g_return_val_if_fail (error == NULL || *error == NULL, NULL); @@ -148,19 +148,21 @@ nmc_string_is_valid (const char *input, const char **allowed, GError **error) if (g_ascii_strncasecmp (input, *p, input_ln) == 0) { if (input_ln == p_len) { ret = *p; + ambiguous = FALSE; break; } - if (!prev_match) + if (!prev_match) { ret = *p; - else { - g_set_error (error, 1, 1, _("'%s' is ambiguous (%s x %s)"), - input, ret, *p); - return NULL; - } - prev_match = TRUE; + prev_match = TRUE; + } else + ambiguous = TRUE; } } - + if (ambiguous) { + g_set_error (error, 1, 1, _("'%s' is ambiguous (%s x %s)"), + input, ret, *p); + return NULL; + } finish: if (ret == NULL) { char *valid_vals = g_strjoinv (", ", (char **) allowed);