Don't create dangling references

Apparently things die at the end of blk scopes.
This commit is contained in:
2026-01-24 14:26:24 -05:00
parent 19d4e88c33
commit ba8a84c478
3 changed files with 41 additions and 60 deletions

View File

@@ -22,10 +22,8 @@ pub fn deinit(self: *Client) void {
}
pub fn sendRelay(self: *Client, io: Io, payload: []const u8, dest: [4]u8) !void {
const rand = blk: {
const io_source: std.Random.IoSource = .{ .io = io };
break :blk io_source.interface();
};
const io_source: std.Random.IoSource = .{ .io = io };
const rand = io_source.interface();
var headers: EthIpUdp = .{
.src_mac = self.socket.mac,
@@ -53,22 +51,18 @@ pub fn sendRelay(self: *Client, io: Io, payload: []const u8, dest: [4]u8) !void
const relay_bytes = relay.toBytes(&relay_buf);
headers.setPayloadLen(relay_bytes.len);
const full_msg = blk: {
var msg_buf: [max_message_size]u8 = undefined;
var msg_w: Writer = .fixed(&msg_buf);
msg_w.writeAll(&headers.toBytes()) catch unreachable;
msg_w.writeAll(relay_bytes) catch unreachable;
break :blk msg_w.buffered();
};
var msg_buf: [max_message_size]u8 = undefined;
var msg_w: Writer = .fixed(&msg_buf);
msg_w.writeAll(&headers.toBytes()) catch unreachable;
msg_w.writeAll(relay_bytes) catch unreachable;
const full_msg = msg_w.buffered();
try self.socket.send(full_msg);
}
pub fn connect(self: Client, io: Io, payload: []const u8) !SaprusConnection {
const rand = blk: {
const io_source: std.Random.IoSource = .{ .io = io };
break :blk io_source.interface();
};
const io_source: std.Random.IoSource = .{ .io = io };
const rand = io_source.interface();
var headers: EthIpUdp = .{
.src_mac = self.socket.mac,
@@ -106,13 +100,11 @@ pub fn connect(self: Client, io: Io, payload: []const u8) !SaprusConnection {
headers.setPayloadLen(connection_bytes.len);
log.debug("Building full message", .{});
var full_msg = blk: {
var msg_buf: [2048]u8 = undefined;
var msg_w: Writer = .fixed(&msg_buf);
msg_w.writeAll(&headers.toBytes()) catch unreachable;
msg_w.writeAll(connection_bytes) catch unreachable;
break :blk msg_w.buffered();
};
var msg_buf: [2048]u8 = undefined;
var msg_w: Writer = .fixed(&msg_buf);
msg_w.writeAll(&headers.toBytes()) catch unreachable;
msg_w.writeAll(connection_bytes) catch unreachable;
var full_msg = msg_w.buffered();
log.debug("Built full message. Sending message", .{});
try self.socket.send(full_msg);
@@ -128,13 +120,13 @@ pub fn connect(self: Client, io: Io, payload: []const u8) !SaprusConnection {
headers.setPayloadLen(connection_bytes.len);
log.debug("Building final handshake message", .{});
full_msg = blk: {
var msg_buf: [2048]u8 = undefined;
var msg_w: Writer = .fixed(&msg_buf);
msg_w.writeAll(&headers.toBytes()) catch unreachable;
msg_w.writeAll(connection_bytes) catch unreachable;
break :blk msg_w.buffered();
};
msg_w.end = 0;
msg_w.writeAll(&headers.toBytes()) catch unreachable;
msg_w.writeAll(connection_bytes) catch unreachable;
full_msg = msg_w.buffered();
try self.socket.send(full_msg);
return .init(self.socket, headers, connection);