reorganize but crashing

not sure why, seems like i'm using the right allocators everywhere?
need to take another pass at this later.
This commit is contained in:
2026-01-02 03:00:49 +00:00
parent a71f08e1f6
commit 4bf064056c
3 changed files with 70 additions and 46 deletions

View File

@@ -102,24 +102,22 @@ pub const Message = union(MessageType) {
}
pub fn dupe(self: Connect, alloc: std.mem.Allocator) !Connect {
return .{
.verbose = self.verbose,
.pedantic = self.pedantic,
.tls_required = self.tls_required,
.auth_token = if (self.auth_token) |a| try alloc.dupe(u8, a) else null,
.user = if (self.user) |u| try alloc.dupe(u8, u) else null,
.pass = if (self.pass) |p| try alloc.dupe(u8, p) else null,
.name = if (self.name) |n| try alloc.dupe(u8, n) else null,
.lang = self.lang,
.version = self.version,
.protocol = self.protocol,
.echo = self.echo,
.sig = if (self.sig) |s| try alloc.dupe(u8, s) else null,
.jwt = if (self.jwt) |j| try alloc.dupe(u8, j) else null,
.no_responders = self.no_responders,
.headers = self.headers,
.nkey = if (self.nkey) |n| try alloc.dupe(u8, n) else null,
};
var res = self;
res.auth_token = if (self.auth_token) |a| try alloc.dupe(u8, a) else null;
errdefer if (res.auth_token) |a| alloc.free(a);
res.user = if (self.user) |u| try alloc.dupe(u8, u) else null;
errdefer if (res.user) |u| alloc.free(u);
res.pass = if (self.pass) |p| try alloc.dupe(u8, p) else null;
errdefer if (res.pass) |p| alloc.free(p);
res.name = if (self.name) |n| try alloc.dupe(u8, n) else null;
errdefer if (res.name) |n| alloc.free(n);
res.sig = if (self.sig) |s| try alloc.dupe(u8, s) else null;
errdefer if (res.sig) |s| alloc.free(s);
res.jwt = if (self.jwt) |j| try alloc.dupe(u8, j) else null;
errdefer if (res.jwt) |j| alloc.free(j);
res.nkey = if (self.nkey) |n| try alloc.dupe(u8, n) else null;
errdefer if (res.nkey) |n| alloc.free(n);
return res;
}
};
pub const Pub = struct {
@@ -165,17 +163,21 @@ pub const Message = union(MessageType) {
pub fn deinit(self: Msg, alloc: std.mem.Allocator) void {
alloc.free(self.subject);
alloc.free(self.sid);
alloc.free(self.payload);
if (self.reply_to) |r| alloc.free(r);
alloc.free(self.payload);
}
pub fn dupe(self: Msg, alloc: std.mem.Allocator) !Msg {
return .{
.subject = try alloc.dupe(u8, self.subject),
.sid = try alloc.dupe(u8, self.sid),
.reply_to = if (self.reply_to) |r| try alloc.dupe(u8, r) else null,
.payload = try alloc.dupe(u8, self.payload),
};
var res: Msg = undefined;
res.subject = try alloc.dupe(u8, self.subject);
errdefer alloc.free(res.subject);
res.sid = try alloc.dupe(u8, self.sid);
errdefer alloc.free(res.sid);
res.reply_to = if (self.reply_to) |r| alloc.dupe(u8, r) else null;
errdefer if (res.reply_to) |r| alloc.free(r);
res.payload = try alloc.dupe(u8, self.payload);
errdefer alloc.free(res.payload);
return res;
}
};
@@ -221,11 +223,16 @@ pub const Message = union(MessageType) {
switch (operation) {
.connect => {
// for storing the json string
var connect_string_writer_allocating: std.Io.Writer.Allocating = try .initCapacity(alloc, 1024);
defer connect_string_writer_allocating.deinit();
var connect_string_writer = connect_string_writer_allocating.writer;
// for parsing the json string
var connect_arena_allocator: std.heap.ArenaAllocator = .init(alloc);
defer connect_arena_allocator.deinit();
const connect_allocator = connect_arena_allocator.allocator();
const connect_string_writer_allocating: std.Io.Writer.Allocating = try .initCapacity(connect_allocator, 1024);
var connect_string_writer = connect_string_writer_allocating.writer;
try in.discardAll(1); // throw away space
// Should read the next JSON object to the fixed buffer writer.
@@ -234,7 +241,12 @@ pub const Message = union(MessageType) {
try expectStreamBytes(in, "}\r\n"); // discard '}\r\n'
// TODO: should be CONNECTION allocator
const res = try std.json.parseFromSliceLeaky(Connect, connect_allocator, connect_string_writer.buffered(), .{ .allocate = .alloc_always });
const res = try std.json.parseFromSliceLeaky(
Connect,
connect_allocator,
connect_string_writer.buffered(),
.{ .allocate = .alloc_always },
);
return .{ .connect = try res.dupe(alloc) };
},
@@ -243,6 +255,7 @@ pub const Message = union(MessageType) {
// Parse subject
const subject: []const u8 = try readSubject(alloc, in);
errdefer alloc.free(subject);
// Parse byte count
const byte_count = blk: {
@@ -267,6 +280,7 @@ pub const Message = union(MessageType) {
const payload = blk: {
const bytes = try alloc.alloc(u8, byte_count);
errdefer alloc.free(bytes);
try in.readSliceAll(bytes);
try expectStreamBytes(in, "\r\n");
break :blk bytes;