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);

View File

@@ -17,10 +17,8 @@ pub fn next(self: Connection, io: Io, buf: []u8) ![]const u8 {
log.debug("Awaiting connection message", .{});
const res = try self.socket.receive(buf);
log.debug("Received {} byte connection message", .{res.len});
const connection_res = blk: {
const msg: SaprusMessage = try .parse(res[42..]);
break :blk msg.connection;
};
const msg: SaprusMessage = try .parse(res[42..]);
const connection_res = msg.connection;
log.debug("Payload was {s}", .{connection_res.payload});
@@ -28,29 +26,23 @@ pub fn next(self: Connection, io: Io, buf: []u8) ![]const u8 {
}
pub fn send(self: *Connection, io: Io, buf: []const 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();
log.debug("Sending connection message", .{});
self.connection.connection.payload = buf;
const connection_bytes = blk: {
var connection_bytes: [2048]u8 = undefined;
break :blk self.connection.toBytes(&connection_bytes);
};
var connection_bytes_buf: [2048]u8 = undefined;
const connection_bytes = self.connection.toBytes(&connection_bytes_buf);
self.headers.ip.id = rand.int(u16);
self.headers.setPayloadLen(connection_bytes.len);
const full_msg = blk: {
var msg_buf: [2048]u8 = undefined;
var msg_w: Writer = .fixed(&msg_buf);
try msg_w.writeAll(&self.headers.toBytes());
try msg_w.writeAll(connection_bytes);
break :blk msg_w.buffered();
};
var msg_buf: [2048]u8 = undefined;
var msg_w: Writer = .fixed(&msg_buf);
try msg_w.writeAll(&self.headers.toBytes());
try msg_w.writeAll(connection_bytes);
const full_msg = msg_w.buffered();
try self.socket.send(full_msg);

View File

@@ -66,12 +66,7 @@ pub fn main(init: std.process.Init) !void {
.connect => {
i += 1;
if (i < args.len) {
var w: Writer = blk: {
var buf: [2048]u8 = undefined;
break :blk .fixed(&buf);
};
try w.printBase64(args[i]);
flags.connect = w.buffered();
flags.connect = args[i];
} else {
flags.connect = "";
}
@@ -130,7 +125,12 @@ pub fn main(init: std.process.Init) !void {
if (flags.connect != null) {
reconnect: while (true) {
log.debug("Starting connection", .{});
var connection = try client.connect(init.io, flags.connect.?);
var init_con_buf: [SaprusClient.max_payload_len]u8 = undefined;
var w: Writer = .fixed(&init_con_buf);
try w.print("{b64}", .{flags.connect.?});
var connection = try client.connect(init.io, w.buffered());
log.debug("Connection started", .{});
while (true) {
@@ -158,11 +158,8 @@ pub fn main(init: std.process.Init) !void {
try child.collectOutput(init.gpa, &child_stdout, &child_stderr, std.math.maxInt(usize));
// const b64e = std.base64.standard.Encoder;
var cmd_output: Writer = blk: {
var cmd_output_buf: [2048]u8 = undefined;
break :blk .fixed(&cmd_output_buf);
};
var cmd_output_buf: [2048]u8 = undefined;
var cmd_output: Writer = .fixed(&cmd_output_buf);
var cmd_output_window_iter = std.mem.window(u8, child_stdout.items, SaprusClient.max_payload_len, SaprusClient.max_payload_len);
while (cmd_output_window_iter.next()) |chunk| {