Skip to content
This repository was archived by the owner on Apr 9, 2025. It is now read-only.

Commit ea422e0

Browse files
committed
fix mem leak on conn reuse
1 parent 0ed7a45 commit ea422e0

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/std/http/Client.zig

+22-2
Original file line numberDiff line numberDiff line change
@@ -1695,6 +1695,7 @@ fn setConnection(ctx: *Ctx, res: anyerror!void) !void {
16951695
.port = ctx.data.conn.port,
16961696
},
16971697
};
1698+
16981699
// remove old pointer, now useless
16991700
const old_conn = ctx.data.conn;
17001701
defer ctx.req.client.allocator.destroy(old_conn);
@@ -1777,6 +1778,11 @@ pub fn async_connectTcp(
17771778
.port = port,
17781779
.protocol = protocol,
17791780
})) |conn| {
1781+
// remove old ctx pointer, now useless
1782+
const old_conn = ctx.data.conn;
1783+
defer ctx.req.client.allocator.destroy(old_conn);
1784+
defer ctx.req.client.allocator.free(old_conn.host);
1785+
17801786
ctx.data.conn = conn;
17811787
ctx.req.connection = conn;
17821788
return ctx.pop({});
@@ -2256,7 +2262,13 @@ pub fn async_open(
22562262

22572263
// add fields to connection
22582264
ctx.data.conn.protocol = protocol;
2265+
2266+
// free the previous host
2267+
client.allocator.free(ctx.data.conn.host);
2268+
22592269
ctx.data.conn.host = try client.allocator.dupe(u8, host.raw);
2270+
errdefer client.allocator.free(ctx.data.conn.host);
2271+
22602272
ctx.data.conn.port = port;
22612273

22622274
return client.async_connect(host.raw, port, protocol, ctx, setRequestConnection);
@@ -2419,14 +2431,22 @@ pub const Ctx = struct {
24192431
_tls_write_buf: [cipher.max_ciphertext_record_len]u8 = undefined,
24202432

24212433
pub fn init(io: *IO, req: *Request) !Ctx {
2422-
const connection = try req.client.allocator.create(Connection);
2434+
const allocator = req.client.allocator;
2435+
2436+
const connection = try allocator.create(Connection);
2437+
errdefer allocator.destroy(connection);
2438+
2439+
const host = try allocator.dupe(u8, "");
2440+
errdefer allocator.free(host);
2441+
24232442
connection.* = .{
24242443
.stream = undefined,
24252444
.tls_client = undefined,
24262445
.protocol = undefined,
2427-
.host = undefined,
2446+
.host = host,
24282447
.port = undefined,
24292448
};
2449+
24302450
return .{
24312451
.req = req,
24322452
.io = io,

0 commit comments

Comments
 (0)