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

Commit 8e4b7aa

Browse files
Merge pull request #10 from lightpanda-io/invalid-free
Invalid free
2 parents 209c4f4 + ea422e0 commit 8e4b7aa

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
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,

src/tests.zig

+12-1
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,19 @@ pub fn onRequestConnect(ctx: *Ctx, res: anyerror!void) anyerror!void {
3737
return ctx.req.async_send(ctx, onRequestSend);
3838
}
3939

40-
test "example.com" {
40+
test "PLAIN example.com" {
4141
var urls = [_][]const u8{
42+
"http://www.example.com",
43+
"http://www.example.com",
44+
"http://www.example.com",
45+
};
46+
try do(&urls);
47+
}
48+
49+
test "TLS example.com" {
50+
var urls = [_][]const u8{
51+
"https://www.example.com",
52+
"https://www.example.com",
4253
"https://www.example.com",
4354
};
4455
try do(&urls);

0 commit comments

Comments
 (0)