Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ip4Address parser: reject 0-prefixed components #9538

Merged
merged 2 commits into from Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/std/net.zig
Expand Up @@ -34,6 +34,7 @@ pub const Address = extern union {
error.InvalidEnd,
error.InvalidCharacter,
error.Incomplete,
error.NonCanonical,
=> {},
}

Expand All @@ -55,6 +56,7 @@ pub const Address = extern union {
error.InvalidEnd,
error.InvalidCharacter,
error.Incomplete,
error.NonCanonical,
=> {},
}

Expand Down Expand Up @@ -204,6 +206,7 @@ pub const Ip4Address = extern struct {
var x: u8 = 0;
var index: u8 = 0;
var saw_any_digits = false;
var has_zero_prefix = false;
for (buf) |c| {
if (c == '.') {
if (!saw_any_digits) {
Expand All @@ -216,7 +219,13 @@ pub const Ip4Address = extern struct {
index += 1;
x = 0;
saw_any_digits = false;
has_zero_prefix = false;
} else if (c >= '0' and c <= '9') {
if (c == '0' and !saw_any_digits) {
has_zero_prefix = true;
} else if (has_zero_prefix) {
return error.NonCanonical;
}
saw_any_digits = true;
x = try std.math.mul(u8, x, 10);
x = try std.math.add(u8, x, c - '0');
Expand Down Expand Up @@ -1149,6 +1158,7 @@ fn linuxLookupNameFromHosts(
error.Incomplete,
error.InvalidIPAddressFormat,
error.InvalidIpv4Mapping,
error.NonCanonical,
=> continue,
};
try addrs.append(LookupAddr{ .addr = addr });
Expand Down
1 change: 1 addition & 0 deletions lib/std/net/test.zig
Expand Up @@ -92,6 +92,7 @@ test "parse and render IPv4 addresses" {
try testing.expectError(error.InvalidEnd, net.Address.parseIp4("127.0.0.1.1", 0));
try testing.expectError(error.Incomplete, net.Address.parseIp4("127.0.0.", 0));
try testing.expectError(error.InvalidCharacter, net.Address.parseIp4("100..0.1", 0));
try testing.expectError(error.NonCanonical, net.Address.parseIp4("127.01.0.1", 0));
}

test "resolve DNS" {
Expand Down