From 0488c3cb52a75ffa4fa73b385d3d6e79cc6da774 Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Sat, 22 Apr 2023 16:34:33 -0700 Subject: [PATCH] std.http: Always initialize `response.headers` in Client.request Before this change, if a request errored before getting its `response.headers` initialized, then it would attempt to `deinit` `response.headers` which would still be `undefined`. Since all locations that set `response.headers` use the same code, it can just be done upfront in `request` instead. Closes #15380 --- lib/std/http/Client.zig | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/std/http/Client.zig b/lib/std/http/Client.zig index 4ff29a215a..f813b729ce 100644 --- a/lib/std/http/Client.zig +++ b/lib/std/http/Client.zig @@ -645,7 +645,6 @@ pub const Request = struct { if (req.response.parser.state.isContent()) break; } - req.response.headers = http.Headers{ .allocator = req.client.allocator, .owned = false }; try req.response.parse(req.response.parser.header_bytes.items); if (req.response.status == .switching_protocols) { @@ -765,7 +764,7 @@ pub const Request = struct { } if (has_trail) { - req.response.headers = http.Headers{ .allocator = req.client.allocator, .owned = false }; + req.response.headers.clearRetainingCapacity(); // The response headers before the trailers are already guaranteed to be valid, so they will always be parsed again and cannot return an error. // This will *only* fail for a malformed trailer. @@ -1019,7 +1018,7 @@ pub fn request(client: *Client, method: http.Method, uri: Uri, headers: http.Hea .status = undefined, .reason = undefined, .version = undefined, - .headers = undefined, + .headers = http.Headers{ .allocator = client.allocator, .owned = false }, .parser = switch (options.header_strategy) { .dynamic => |max| proto.HeadersParser.initDynamic(max), .static => |buf| proto.HeadersParser.initStatic(buf),