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.
This commit is contained in:
Luis Cáceres
2023-08-30 22:39:15 +00:00
committed by Veikka Tuominen
parent e980bd0aee
commit 8976ad7ecb
2 changed files with 6 additions and 0 deletions

View File

@@ -406,6 +406,9 @@ pub const Ip6Address = extern struct {
if (!saw_any_digits and !abbrv) {
return error.Incomplete;
}
if (!abbrv and index < 14) {
return error.Incomplete;
}
if (index == 14) {
ip_slice[14] = @as(u8, @truncate(x >> 8));

View File

@@ -13,6 +13,7 @@ test "parse and render IPv6 addresses" {
"FF01::Fb",
"::1",
"::",
"1::",
"2001:db8::",
"::1234:5678",
"2001:db8::1234:5678",
@@ -24,6 +25,7 @@ test "parse and render IPv6 addresses" {
"ff01::fb",
"::1",
"::",
"1::",
"2001:db8::",
"::1234:5678",
"2001:db8::1234:5678",
@@ -48,6 +50,7 @@ test "parse and render IPv6 addresses" {
try testing.expectError(error.InvalidEnd, net.Address.parseIp6("FF01:0:0:0:0:0:0:FB:", 0));
try testing.expectError(error.Incomplete, net.Address.parseIp6("FF01:", 0));
try testing.expectError(error.InvalidIpv4Mapping, net.Address.parseIp6("::123.123.123.123", 0));
try testing.expectError(error.Incomplete, net.Address.parseIp6("1", 0));
// TODO Make this test pass on other operating systems.
if (builtin.os.tag == .linux or comptime builtin.os.tag.isDarwin()) {
try testing.expectError(error.Incomplete, net.Address.resolveIp6("ff01::fb%", 0));