diff --git a/lib/std/Uri.zig b/lib/std/Uri.zig index 352af5aed73d4bd11ed7302b44669d52229d9f9d..3ef546aee86873f60c81303e31689d3900d6b244 100644 --- a/lib/std/Uri.zig +++ b/lib/std/Uri.zig @@ -197,12 +197,7 @@ pub fn percentDecodeInPlace(buffer: []u8) []u8 { return percentDecodeBackwards(buffer, buffer); } -pub const ParseError = error{ - UnexpectedCharacter, - InvalidFormat, - InvalidPort, - InvalidHostName, -}; +pub const ParseError = error{ UnexpectedCharacter, InvalidFormat, InvalidPort, InvalidHostName }; /// Parses the URI or returns an error. This function is not compliant, but is required to parse /// some forms of URIs in the wild, such as HTTP Location headers. @@ -264,7 +259,9 @@ pub fn parseAfterScheme(scheme: []const u8, text: []const u8) ParseError!Uri { } if (start_of_host >= end_of_host) return error.InvalidFormat; - uri.host = .{ .percent_encoded = authority[start_of_host..end_of_host] }; + const host = authority[start_of_host..end_of_host]; + if (host.len > HostName.max_len) return error.InvalidHostName; + uri.host = .{ .percent_encoded = host }; } const path_start = i; @@ -594,6 +591,11 @@ test "should fail gracefully" { try std.testing.expectError(error.InvalidFormat, parse("foobar://")); } +test "parse name too long" { + const uri = "http://" ++ @as([HostName.max_len + 1]u8, @splat('Z')); + try std.testing.expectError(error.InvalidHostName, parse(uri)); +} + test "file" { const parsed = try parse("file:///"); try std.testing.expectEqualStrings("file", parsed.scheme);