]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
IPv6 address parser: fail on incomplete addresses
authorMaria Matejka <mq@ucw.cz>
Wed, 14 Aug 2019 10:29:04 +0000 (12:29 +0200)
committerMaria Matejka <mq@ucw.cz>
Fri, 1 May 2020 13:19:12 +0000 (15:19 +0200)
lib/ip.c
lib/ip_test.c
test/birdtest.c

index 2d1951606960414e9ba46ba3cf9abf4e8f1a4796..fcc72cafb4de5ee8e2c2c1fcafae24c1f6812399 100644 (file)
--- a/lib/ip.c
+++ b/lib/ip.c
@@ -264,6 +264,9 @@ ip6_pton(const char *a, ip6_addr *o)
   int i, j, k, l, hfil;
   const char *start;
 
+  if (!a[0])                           /* Empty string check */
+    return 0;
+
   if (a[0] == ':')                     /* Leading :: */
   {
     if (a[1] != ':')
@@ -333,6 +336,8 @@ ip6_pton(const char *a, ip6_addr *o)
     for (; i>=hfil; i--)
       words[i] = 0;
   }
+  else if (i != 8)     /* Incomplete address */
+    return 0;
 
   /* Convert the address to ip6_addr format */
   for (i=0; i<4; i++)
index fd70c95792d813c2df54ad0f7111deb4d2018af5..36d10d6893e1a3354501fe3a9d279a5c3fa60f6a 100644 (file)
 #define IP4_MAX_LEN            16
 
 static int
-test_ipa_pton(void *out_, const void *in_, const void *expected_out_)
+test_ip4_pton(void *out_, const void *in_, const void *expected_out_)
 {
   ip_addr *out = out_;
   const char *in = in_;
   const ip_addr *expected_out = expected_out_;
+  ip4_addr ip4;
 
-  if (ipa_is_ip4(*expected_out))
+  if (expected_out)
   {
-    ip4_addr ip4;
     bt_assert(ip4_pton(in, &ip4));
     *out = ipa_from_ip4(ip4);
+    return ipa_equal(*out, *expected_out);
   }
   else
+    return !ip4_pton(in, &ip4);
+
+}
+
+static int
+test_ip6_pton(void *out_, const void *in_, const void *expected_out_)
+{
+  ip_addr *out = out_;
+  const char *in = in_;
+  const ip_addr *expected_out = expected_out_;
+
+  if (expected_out)
   {
     bt_assert(ip6_pton(in, out));
-    /* ip_addr == ip6_addr */
+    return ipa_equal(*out, *expected_out);
   }
-
-  return ipa_equal(*out, *expected_out);
+  else
+    return !ip6_pton(in, out);
 }
 
 static int
@@ -52,7 +65,7 @@ t_ip4_pton(void)
     },
   };
 
-  return bt_assert_batch(test_vectors, test_ipa_pton, bt_fmt_str, bt_fmt_ipa);
+  return bt_assert_batch(test_vectors, test_ip4_pton, bt_fmt_str, bt_fmt_ipa);
 }
 
 static int
@@ -87,9 +100,17 @@ t_ip6_pton(void)
       .in  = "2605:2700:0:3::4713:93e3",
       .out = & ipa_build6(0x26052700, 0x00000003, 0x00000000, 0x471393E3),
     },
+    {
+      .in = "2605:2700:0:3:4713:93e3",
+      .out = NULL,
+    },
+    {
+      .in = "2",
+      .out = NULL,
+    },
   };
 
-  return bt_assert_batch(test_vectors, test_ipa_pton, bt_fmt_str, bt_fmt_ipa);
+  return bt_assert_batch(test_vectors, test_ip6_pton, bt_fmt_str, bt_fmt_ipa);
 }
 
 static int
index a092446a6a9b65ada63ad44c71bbfeb534d3bd57..641fd3c79689569e809eb336649e08b3cc230c1c 100644 (file)
@@ -495,7 +495,10 @@ void
 bt_fmt_ipa(char *buf, size_t size, const void *data)
 {
   const ip_addr *ip = data;
-  bsnprintf(buf, size, "%I", *ip);
+  if (data)
+    bsnprintf(buf, size, "%I", *ip);
+  else
+    bsnprintf(buf, size, "(null)");
 }
 
 int