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

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