]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
countries: Fix matching invalid country codes
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 10 Jun 2021 09:37:22 +0000 (09:37 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 10 Jun 2021 10:08:49 +0000 (10:08 +0000)
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 <michael.tremer@ipfire.org>
src/country-list.c
src/country.c

index cc367405295e0eaa0099091fcf0b9824a8acf531..103536cdc52f2d7599734c1310622c867dc54258 100644 (file)
@@ -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);
index 7aac0dba3ae50acffffb52284cdcfeb7fc73fe92..3ad3aacdaee8fb642442473ff22896004a7aa68c 100644 (file)
@@ -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)