From e73b7709ea09ebe108ea3427e27ce52eafa97093 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 10 Jun 2021 09:37:22 +0000 Subject: [PATCH] countries: Fix matching invalid country codes When an invalid country code is entered, loc_country_new returns an error which is interpreted as a match to the list since we check for a non-zero return code. Any invalid country codes are now silently ignored and not considered a match. Fixes: #12620 - "location list-networks-by-cc" returns garbage Signed-off-by: Michael Tremer --- src/country-list.c | 9 +++++++-- src/country.c | 7 +++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/country-list.c b/src/country-list.c index cc36740..103536c 100644 --- a/src/country-list.c +++ b/src/country-list.c @@ -151,8 +151,13 @@ LOC_EXPORT int loc_country_list_contains_code( struct loc_country* country; int r = loc_country_new(list->ctx, &country, code); - if (r) - return -1; + if (r) { + // Ignore invalid country codes which would never match + if (errno == EINVAL) + return 0; + else + return r; + } r = loc_country_list_contains(list, country); loc_country_unref(country); diff --git a/src/country.c b/src/country.c index 7aac0db..3ad3aac 100644 --- a/src/country.c +++ b/src/country.c @@ -34,8 +34,11 @@ struct loc_country { }; LOC_EXPORT int loc_country_new(struct loc_ctx* ctx, struct loc_country** country, const char* country_code) { - if (!loc_country_code_is_valid(country_code)) - return -EINVAL; + // Check of the country code is valid + if (!loc_country_code_is_valid(country_code)) { + errno = EINVAL; + return 1; + } struct loc_country* c = calloc(1, sizeof(*c)); if (!c) -- 2.39.2