authorgravatar for lacc97@protonmail.chLuis Cáceres <lacc97@protonmail.ch> 2023-08-30 22:39:15+00:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-09-06 11:14:24+03:00
log8976ad7ecbd6a0c70749e3bef9c508d0bfde02d2
tree785a2a299ab568bbd563f7991ca333d9e229cc73
parente980bd0aeedff7841882373b42a036c1c985e28f

std.net: Fix IPv6 address parsing for single digit

This fixes the case where IPv6 address parsing incorrectly succeeded on input such as `1`, which now returns error.Incomplete.

2 files changed, 6 insertions(+), 0 deletions(-)

lib/std/net.zig+3
...@@ -406,6 +406,9 @@ pub const Ip6Address = extern struct {...@@ -406,6 +406,9 @@ pub const Ip6Address = extern struct {
406 if (!saw_any_digits and !abbrv) {406 if (!saw_any_digits and !abbrv) {
407 return error.Incomplete;407 return error.Incomplete;
408 }408 }
409 if (!abbrv and index < 14) {
410 return error.Incomplete;
411 }
409412
410 if (index == 14) {413 if (index == 14) {
411 ip_slice[14] = @as(u8, @truncate(x >> 8));414 ip_slice[14] = @as(u8, @truncate(x >> 8));
lib/std/net/test.zig+3
...@@ -13,6 +13,7 @@ test "parse and render IPv6 addresses" {...@@ -13,6 +13,7 @@ test "parse and render IPv6 addresses" {
13 "FF01::Fb",13 "FF01::Fb",
14 "::1",14 "::1",
15 "::",15 "::",
16 "1::",
16 "2001:db8::",17 "2001:db8::",
17 "::1234:5678",18 "::1234:5678",
18 "2001:db8::1234:5678",19 "2001:db8::1234:5678",
...@@ -24,6 +25,7 @@ test "parse and render IPv6 addresses" {...@@ -24,6 +25,7 @@ test "parse and render IPv6 addresses" {
24 "ff01::fb",25 "ff01::fb",
25 "::1",26 "::1",
26 "::",27 "::",
28 "1::",
27 "2001:db8::",29 "2001:db8::",
28 "::1234:5678",30 "::1234:5678",
29 "2001:db8::1234:5678",31 "2001:db8::1234:5678",
...@@ -48,6 +50,7 @@ test "parse and render IPv6 addresses" {...@@ -48,6 +50,7 @@ test "parse and render IPv6 addresses" {
48 try testing.expectError(error.InvalidEnd, net.Address.parseIp6("FF01:0:0:0:0:0:0:FB:", 0));50 try testing.expectError(error.InvalidEnd, net.Address.parseIp6("FF01:0:0:0:0:0:0:FB:", 0));
49 try testing.expectError(error.Incomplete, net.Address.parseIp6("FF01:", 0));51 try testing.expectError(error.Incomplete, net.Address.parseIp6("FF01:", 0));
50 try testing.expectError(error.InvalidIpv4Mapping, net.Address.parseIp6("::123.123.123.123", 0));52 try testing.expectError(error.InvalidIpv4Mapping, net.Address.parseIp6("::123.123.123.123", 0));
53 try testing.expectError(error.Incomplete, net.Address.parseIp6("1", 0));
51 // TODO Make this test pass on other operating systems.54 // TODO Make this test pass on other operating systems.
52 if (builtin.os.tag == .linux or comptime builtin.os.tag.isDarwin()) {55 if (builtin.os.tag == .linux or comptime builtin.os.tag.isDarwin()) {
53 try testing.expectError(error.Incomplete, net.Address.resolveIp6("ff01::fb%", 0));56 try testing.expectError(error.Incomplete, net.Address.resolveIp6("ff01::fb%", 0));