authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-10-13 18:59:40-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-10-29 06:20:49-07:00
log539808239ecae050e383dd11f395a363b4404e21
tree23aba7596794d31d96c19a3f1eef3b0db3694599
parentd3f0c460ecb0ca5b1ba9c57f443463e1f3f49012

std.net: IPv6 parsing fixes


2 files changed, 35 insertions(+), 21 deletions(-)

lib/std/Io/net.zig+21-11
......@@ -70,7 +70,7 @@ pub const IpAddress = union(enum) {
7070 pub fn parseLiteral(text: []const u8) ParseLiteralError!IpAddress {
7171 if (text.len == 0) return error.InvalidAddress;
7272 if (text[0] == '[') {
73 const addr_end = std.mem.indexOfScalar(u8, text, ']') orelse
73 const addr_end = std.mem.findScalar(u8, text, ']') orelse
7474 return error.InvalidAddress;
7575 const addr_text = text[1..addr_end];
7676 const port: u16 = p: {
......@@ -80,7 +80,7 @@ pub const IpAddress = union(enum) {
8080 };
8181 return parseIp6(addr_text, port) catch error.InvalidAddress;
8282 }
83 if (std.mem.indexOfScalar(u8, text, ':')) |i| {
83 if (std.mem.findScalar(u8, text, ':')) |i| {
8484 const addr = Ip4Address.parse(text[0..i], 0) catch return error.InvalidAddress;
8585 return .{ .ip4 = .{
8686 .bytes = addr.bytes,
......@@ -431,17 +431,22 @@ pub const Ip6Address = struct {
431431 pub const Parsed = union(enum) {
432432 success: Unresolved,
433433 invalid_byte: usize,
434 unexpected_end,
434 incomplete,
435435 junk_after_end: usize,
436436 interface_name_oversized: usize,
437 invalid_ip4_mapping: usize,
438 overflow: usize,
437439 };
438440
439441 pub fn parse(text: []const u8) Parsed {
440 if (text.len < 2) return .unexpected_end;
441 if (std.ascii.startsWithIgnoreCase(text, "::ffff:")) ip4_mapped: {
442 const a4 = (Ip4Address.parse(text["::ffff:".len..], 0) catch break :ip4_mapped).bytes;
442 if (text.len < 2) return .incomplete;
443 const ip4_prefix = "::ffff:";
444 if (std.ascii.startsWithIgnoreCase(text, ip4_prefix)) {
445 const parsed = Ip4Address.parse(text[ip4_prefix.len..], 0) catch
446 return .{ .invalid_ip4_mapping = ip4_prefix.len };
447 const b = parsed.bytes;
443448 return .{ .success = .{
444 .bytes = .{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, a4[0], a4[1], a4[2], a4[3] },
449 .bytes = .{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, b[0], b[1], b[2], b[3] },
445450 .interface_name = null,
446451 } };
447452 }
......@@ -457,7 +462,9 @@ pub const Ip6Address = struct {
457462 .digit => c: switch (text[text_i]) {
458463 'a'...'f' => |c| {
459464 const digit = c - 'a' + 10;
460 parts[parts_i] = parts[parts_i] * 16 + digit;
465 parts[parts_i] = (std.math.mul(u16, parts[parts_i], 16) catch return .{
466 .overflow = text_i,
467 }) + digit;
461468 if (digit_i == 4) return .{ .invalid_byte = text_i };
462469 digit_i += 1;
463470 text_i += 1;
......@@ -470,7 +477,9 @@ pub const Ip6Address = struct {
470477 'A'...'F' => |c| continue :c c - 'A' + 'a',
471478 '0'...'9' => |c| {
472479 const digit = c - '0';
473 parts[parts_i] = parts[parts_i] * 16 + digit;
480 parts[parts_i] = (std.math.mul(u16, parts[parts_i], 16) catch return .{
481 .overflow = text_i,
482 }) + digit;
474483 if (digit_i == 4) return .{ .invalid_byte = text_i };
475484 digit_i += 1;
476485 text_i += 1;
......@@ -497,7 +506,7 @@ pub const Ip6Address = struct {
497506 if (parts.len - parts_i == 0) continue :state .end;
498507 digit_i = 0;
499508 text_i += 1;
500 if (text.len - text_i == 0) return .unexpected_end;
509 if (text.len - text_i == 0) return .incomplete;
501510 continue :c text[text_i];
502511 }
503512 },
......@@ -507,6 +516,7 @@ pub const Ip6Address = struct {
507516 text_i += 1;
508517 const name = text[text_i..];
509518 if (name.len > Interface.Name.max_len) return .{ .interface_name_oversized = text_i };
519 if (name.len == 0) return .incomplete;
510520 interface_name_text = name;
511521 text_i = @intCast(text.len);
512522 continue :state .end;
......@@ -521,7 +531,7 @@ pub const Ip6Address = struct {
521531 @memmove(parts[parts.len - src.len ..], src);
522532 @memset(parts[s..][0..remaining], 0);
523533 } else {
524 if (remaining != 0) return .unexpected_end;
534 if (remaining != 0) return .incomplete;
525535 }
526536
527537 // Workaround that can be removed when this proposal is
lib/std/Io/net/test.zig+14-10
......@@ -56,6 +56,7 @@ test "parse and render IPv6 addresses" {
5656 try testParseAndRenderIp6Address("2001:db8::1234:5678", "2001:db8::1234:5678");
5757 try testParseAndRenderIp6Address("FF01::FB%1234", "ff01::fb%1234");
5858 try testParseAndRenderIp6Address("::ffff:123.5.123.5", "::ffff:123.5.123.5");
59 try testParseAndRenderIp6Address("ff01::fb%12345678901234", "ff01::fb%12345678901234");
5960}
6061
6162fn testParseAndRenderIp6Address(input: []const u8, expected_output: []const u8) !void {
......@@ -66,16 +67,19 @@ fn testParseAndRenderIp6Address(input: []const u8, expected_output: []const u8)
6667}
6768
6869test "IPv6 address parse failures" {
69 try testing.expectError(error.InvalidCharacter, net.IpAddress.parseIp6(":::", 0));
70 try testing.expectError(error.Overflow, net.IpAddress.parseIp6("FF001::FB", 0));
71 try testing.expectError(error.InvalidCharacter, net.IpAddress.parseIp6("FF01::Fb:zig", 0));
72 try testing.expectError(error.InvalidEnd, net.IpAddress.parseIp6("FF01:0:0:0:0:0:0:FB:", 0));
73 try testing.expectError(error.Incomplete, net.IpAddress.parseIp6("FF01:", 0));
74 try testing.expectError(error.InvalidIpv4Mapping, net.IpAddress.parseIp6("::123.123.123.123", 0));
75 try testing.expectError(error.Incomplete, net.IpAddress.parseIp6("1", 0));
76 try testing.expectError(error.Incomplete, net.IpAddress.parseIp6("ff01::fb%", 0));
77 try testing.expectError(error.Overflow, net.IpAddress.parseIp6("ff01::fb%wlp3" ++ "s0" ** @divExact(std.posix.IFNAMESIZE - 4, 2), 0));
78 try testing.expectError(error.Overflow, net.IpAddress.parseIp6("ff01::fb%12345678901234", 0));
70 try testing.expectError(error.ParseFailed, net.IpAddress.parseIp6(":::", 0));
71
72 const Unresolved = net.Ip6Address.Unresolved;
73
74 try testing.expectEqual(Unresolved.Parsed{ .invalid_byte = 2 }, Unresolved.parse(":::"));
75 try testing.expectEqual(Unresolved.Parsed{ .overflow = 4 }, Unresolved.parse("FF001::FB"));
76 try testing.expectEqual(Unresolved.Parsed{ .invalid_byte = 9 }, Unresolved.parse("FF01::Fb:zig"));
77 try testing.expectEqual(Unresolved.Parsed{ .junk_after_end = 19 }, Unresolved.parse("FF01:0:0:0:0:0:0:FB:"));
78 try testing.expectEqual(Unresolved.Parsed.incomplete, Unresolved.parse("FF01:"));
79 try testing.expectEqual(Unresolved.Parsed{ .invalid_byte = 5 }, Unresolved.parse("::123.123.123.123"));
80 try testing.expectEqual(Unresolved.Parsed.incomplete, Unresolved.parse("1"));
81 try testing.expectEqual(Unresolved.Parsed.incomplete, Unresolved.parse("ff01::fb%"));
82 try testing.expectEqual(Unresolved.Parsed{ .interface_name_oversized = 9 }, Unresolved.parse("ff01::fb%wlp3" ++ "s0" ** @divExact(std.posix.IFNAMESIZE - 4, 2)));
7983}
8084
8185test "invalid but parseable IPv6 scope ids" {