]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/in-addr-util: drop check for prefix length in formatting function
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 3 Jun 2022 13:49:40 +0000 (15:49 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 6 Jun 2022 07:52:52 +0000 (09:52 +0200)
The general rule should be to be strict when parsing data, but lenient
when printing it. Or in other words, we should verify data in verification
functions, but not when printing things. It doesn't make sense to refuse
to print a value that we are using internally.

We were tripping ourselves in some of the print functions:
we want to report than an address was configured with too-long prefix, but
the log line would use "n/a" if the prefix was too long. This is not useful.

Most of the time, the removal of the check doesn't make any difference,
because we verified the prefix length on input.

src/basic/in-addr-util.c
src/test/test-in-addr-prefix-util.c
src/test/test-in-addr-util.c

index 39ae694f20d4bc60545b00ba979fa5c8c57ceaa7..6bf017260bee014eead4543c544f180480f9fa17 100644 (file)
@@ -460,15 +460,12 @@ int in_addr_prefix_to_string(int family, const union in_addr_union *u, unsigned
         assert(ret);
 
         if (family == AF_INET)
-                l = INET_ADDRSTRLEN + 3;
+                l = INET_ADDRSTRLEN + 1 + DECIMAL_STR_MAX(unsigned);
         else if (family == AF_INET6)
-                l = INET6_ADDRSTRLEN + 4;
+                l = INET6_ADDRSTRLEN + 1 + DECIMAL_STR_MAX(unsigned);
         else
                 return -EAFNOSUPPORT;
 
-        if (prefixlen > FAMILY_ADDRESS_SIZE(family) * 8)
-                return -EINVAL;
-
         x = new(char, l);
         if (!x)
                 return -ENOMEM;
index 6503353465cbeb3c4bc61283bfdb7569e161d16a..d2991b9d3d349d9dc5a2ebdfafe1efb3e3181596 100644 (file)
@@ -3,6 +3,29 @@
 #include "in-addr-prefix-util.h"
 #include "tests.h"
 
+static void test_in_addr_prefix_to_string_one(int f, const char *addr, unsigned prefixlen) {
+        union in_addr_union ua;
+        _cleanup_free_ char *r;
+
+        assert_se(in_addr_from_string(f, addr, &ua) >= 0);
+        assert_se(in_addr_prefix_to_string(f, &ua, prefixlen, &r) >= 0);
+        printf("%s: %s/%u == %s\n", __func__, addr, prefixlen, r);
+        assert_se(startswith(r, addr));
+}
+
+TEST(in_addr_to_string_prefix) {
+        test_in_addr_prefix_to_string_one(AF_INET, "192.168.0.1", 0);
+        test_in_addr_prefix_to_string_one(AF_INET, "192.168.0.1", 1);
+        test_in_addr_prefix_to_string_one(AF_INET, "192.168.0.1", 31);
+        test_in_addr_prefix_to_string_one(AF_INET, "192.168.0.1", 32);
+        test_in_addr_prefix_to_string_one(AF_INET, "192.168.0.1", 256);
+        test_in_addr_prefix_to_string_one(AF_INET, "10.11.12.13", UINT_MAX);
+        test_in_addr_prefix_to_string_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 0);
+        test_in_addr_prefix_to_string_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", UINT_MAX);
+        test_in_addr_prefix_to_string_one(AF_INET6, "::1", 11);
+        test_in_addr_prefix_to_string_one(AF_INET6, "fe80::", 33);
+}
+
 static void test_config_parse_in_addr_prefixes_one(int family, const union in_addr_union *addr, uint8_t prefixlen, Set **prefixes) {
         _cleanup_free_ char *str = NULL;
 
index c0808d51b99138529ad03323df62459a75032417..296cd274345f5f781a9ea2a09be28b22c755b829 100644 (file)
@@ -351,7 +351,7 @@ static void test_in_addr_to_string_one(int f, const char *addr) {
 
         assert_se(in_addr_from_string(f, addr, &ua) >= 0);
         assert_se(in_addr_to_string(f, &ua, &r) >= 0);
-        printf("test_in_addr_to_string_one: %s == %s\n", addr, r);
+        printf("%s: %s == %s\n", __func__, addr, r);
         assert_se(streq(addr, r));
 
         assert_se(streq(r, IN_ADDR_TO_STRING(f, &ua)));