From 2113a3ed8217775797dd9a82aa420c10ef1712d5 Mon Sep 17 00:00:00 2001 From: Szabolcs Nagy Date: Sun, 22 Mar 2015 04:51:14 +0000 Subject: [PATCH] inet_pton tests, fix regression test case standard could be clearer about leading and trailing :: --- src/functional/inet_pton.c | 15 +++++--- src/regression/inet_pton-empty-last-field.c | 55 ++++++++++++++++++++--------- 2 files changed, 50 insertions(+), 20 deletions(-) rewrite src/regression/inet_pton-empty-last-field.c (69%) diff --git a/src/functional/inet_pton.c b/src/functional/inet_pton.c index 0857b82..b31faed 100644 --- a/src/functional/inet_pton.c +++ b/src/functional/inet_pton.c @@ -28,9 +28,9 @@ static void tohex(char *d, void *s, int n) #define V6(src,ret,hex) do{\ int r; \ - char binaddr[16]; \ - char hexaddr[40]; \ - char txtaddr[60]; \ + char binaddr[16]={0}; \ + char hexaddr[40]={0}; \ + char txtaddr[60]={0}; \ \ r=inet_pton(AF_INET6,src,binaddr); \ if (r!=ret) \ @@ -57,7 +57,7 @@ static void tohex(char *d, void *s, int n) int r; \ uint32_t a; \ struct in_addr in; \ - char buf[20]; \ + char buf[20]={0}; \ char *p; \ \ a=inet_addr(src); \ @@ -140,6 +140,13 @@ V6(".192.168.1.1", 0, "") V6(":.192.168.1.1", 0, "") V6("a:0b:00c:000d:E:F::", 1, "000a000b000c000d000e000f00000000") V6("a:0b:00c:000d:0000e:f::", 0, "") +V6("1:2:3:4:5:6::", 1, "00010002000300040005000600000000") +V6("1:2:3:4:5:6:7::", 1, "00010002000300040005000600070000") +V6("1:2:3:4:5:6:7:8::", 0, "") +V6("1:2:3:4:5:6:7::9", 0, "") +V6("::1:2:3:4:5:6", 1, "00000000000100020003000400050006") +V6("::1:2:3:4:5:6:7", 1, "00000001000200030004000500060007") +V6("::1:2:3:4:5:6:7:8", 0, "") V6("a:b::c:d:e:f", 1, "000a000b00000000000c000d000e000f") V6("ffff:c0a8:5e4", 0, "") V6(":ffff:c0a8:5e4", 0, "") diff --git a/src/regression/inet_pton-empty-last-field.c b/src/regression/inet_pton-empty-last-field.c dissimilarity index 69% index eef44dc..b06fa11 100644 --- a/src/regression/inet_pton-empty-last-field.c +++ b/src/regression/inet_pton-empty-last-field.c @@ -1,16 +1,39 @@ -// '0' last field in an ipv6 address cannot be abbreviated to :: -#include -#include -#include "test.h" - -int main(void) -{ - unsigned char buf[16]; - char addr[] = "1:2:3:4:5:6:7::"; - - if (inet_pton(AF_INET6, addr, buf)) { - t_error("inet_pton(%s) returned %x:%x:%x:%x:%x:%x:%x:%x, wanted a failure\n", - addr, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]); - } - return t_status; -} +// zero compression for the last field in an ipv6 address is (probably) allowed +// https://tools.ietf.org/html/rfc4291#section-2.2 +// but further fields shouldnt buffer overflow +#include +#include +#include +#include "test.h" + +static void txt(char *s, unsigned char *buf) +{ + int i; + sprintf(s, "%04x", buf[0]<<8 | buf[1]); + for (i=1; i<8; i++) + sprintf(s+5*i, ":%04x", buf[2*i]<<8 | buf[2*i+1]); +} + +int main(void) +{ + char s[50], sw[50]; + unsigned char buf[16]; + unsigned char want[16] = {0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,0}; + char *addr; + + addr = "1:2:3:4:5:6:7::"; + if (inet_pton(AF_INET6, addr, buf)!=1 || memcmp(buf, want, 16)!=0) { + txt(s, buf); + txt(sw, want); + t_error("inet_pton(%s) returned %s, wanted %s\n", + addr, s, sw); + } + + addr = "1:2:3:4:5:6:7::9:10:11:12:13:14:15:16:17:18:19:20"; + if (inet_pton(AF_INET6, addr, buf)!=0) { + txt(s, buf); + t_error("inet_pton(%s) returned %s, wanted a failure\n", + addr, s); + } + return t_status; +} -- 2.11.4.GIT