]> git.ipfire.org Git - location/libloc.git/commitdiff
address: Set default prefix if none is given
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 9 Mar 2022 13:55:48 +0000 (13:55 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 9 Mar 2022 13:55:48 +0000 (13:55 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/address.c

index ff4630e85c1136735d09369a10bcf9f97f0103a0..dd32dbc17d1d7313fb1b1bd0d31536728e35e8c9 100644 (file)
@@ -138,11 +138,22 @@ int loc_address_parse(struct in6_addr* address, unsigned int* prefix, const char
        // Did the user request a prefix?
        if (prefix) {
                // Set the prefix to the default value
-               *prefix = loc_address_family_bit_length(family);
+               const unsigned int max_prefix = loc_address_family_bit_length(family);
 
                // Parse the actual string
-               if (p)
+               if (p) {
                        *prefix = strtol(p, NULL, 10);
+
+                       // Check if prefix is within bounds
+                       if (*prefix > max_prefix) {
+                               errno = EINVAL;
+                               return 1;
+                       }
+
+               // If the string didn't contain a prefix, we set the maximum
+               } else {
+                       *prefix = max_prefix;
+               }
        }
 
        return 0;