Cleanup on error

This commit is contained in:
2026-01-02 13:34:25 +00:00
parent 4bf064056c
commit aca265f095

View File

@@ -111,6 +111,10 @@ pub const Message = union(MessageType) {
errdefer if (res.pass) |p| alloc.free(p); errdefer if (res.pass) |p| alloc.free(p);
res.name = if (self.name) |n| try alloc.dupe(u8, n) else null; res.name = if (self.name) |n| try alloc.dupe(u8, n) else null;
errdefer if (res.name) |n| alloc.free(n); errdefer if (res.name) |n| alloc.free(n);
res.lang = try alloc.dupe(u8, self.lang);
errdefer alloc.free(res.lang);
res.version = try alloc.dupe(u8, self.version);
errdefer alloc.free(res.version);
res.sig = if (self.sig) |s| try alloc.dupe(u8, s) else null; res.sig = if (self.sig) |s| try alloc.dupe(u8, s) else null;
errdefer if (res.sig) |s| alloc.free(s); errdefer if (res.sig) |s| alloc.free(s);
res.jwt = if (self.jwt) |j| try alloc.dupe(u8, j) else null; res.jwt = if (self.jwt) |j| try alloc.dupe(u8, j) else null;
@@ -307,6 +311,7 @@ pub const Message = union(MessageType) {
return error.InvalidStream; return error.InvalidStream;
} }
const subject = try readSubject(alloc, in); const subject = try readSubject(alloc, in);
errdefer alloc.free(subject);
const second = blk: { const second = blk: {
// Drop whitespace // Drop whitespace
while (in.peekByte()) |byte| { while (in.peekByte()) |byte| {
@@ -316,6 +321,7 @@ pub const Message = union(MessageType) {
} else |err| return err; } else |err| return err;
var acc: std.ArrayList(u8) = try .initCapacity(alloc, 32); var acc: std.ArrayList(u8) = try .initCapacity(alloc, 32);
errdefer acc.deinit(alloc);
while (in.peekByte()) |byte| { while (in.peekByte()) |byte| {
if (std.ascii.isWhitespace(byte)) break; if (std.ascii.isWhitespace(byte)) break;
try acc.append(alloc, byte); try acc.append(alloc, byte);
@@ -324,8 +330,12 @@ pub const Message = union(MessageType) {
break :blk try acc.toOwnedSlice(alloc); break :blk try acc.toOwnedSlice(alloc);
}; };
errdefer alloc.free(second);
const queue_group = if ((try in.peekByte()) != '\r') second else null; const queue_group = if ((try in.peekByte()) != '\r') second else null;
const sid = if (queue_group) |_| try in.takeDelimiterExclusive('\r') else second; // We do not need an errdefer free for queue group, because it will only be second (already has errdefer free) or null.
const sid = if (queue_group) |_| try alloc.dupe(u8, try in.takeDelimiterExclusive('\r')) else second;
// if queue_group is null, that means sid is second, and already has an errdefer free.
errdefer if (queue_group) |_| alloc.free(sid);
try expectStreamBytes(in, "\r\n"); try expectStreamBytes(in, "\r\n");
return .{ return .{
.sub = .{ .sub = .{
@@ -340,9 +350,10 @@ pub const Message = union(MessageType) {
@branchHint(.unlikely); @branchHint(.unlikely);
return error.InvalidStream; return error.InvalidStream;
} }
// Parse byte count // Parse sid
const sid = blk: { const sid = blk: {
var acc: std.ArrayList(u8) = try .initCapacity(alloc, 8); var acc: std.ArrayList(u8) = try .initCapacity(alloc, 8);
errdefer acc.deinit(alloc);
while (in.peekByte()) |byte| { while (in.peekByte()) |byte| {
if (std.ascii.isWhitespace(byte)) break; if (std.ascii.isWhitespace(byte)) break;
try acc.append(alloc, byte); try acc.append(alloc, byte);
@@ -350,6 +361,7 @@ pub const Message = union(MessageType) {
} else |err| return err; } else |err| return err;
break :blk try acc.toOwnedSlice(alloc); break :blk try acc.toOwnedSlice(alloc);
}; };
errdefer alloc.free(sid);
if ((try in.peekByte()) == '\r') { if ((try in.peekByte()) == '\r') {
try expectStreamBytes(in, "\r\n"); try expectStreamBytes(in, "\r\n");
@@ -362,6 +374,7 @@ pub const Message = union(MessageType) {
in.toss(1); in.toss(1);
const max_msgs = blk: { const max_msgs = blk: {
var max_msgs_list: std.ArrayList(u8) = try .initCapacity(alloc, 64); var max_msgs_list: std.ArrayList(u8) = try .initCapacity(alloc, 64);
errdefer max_msgs_list.deinit(alloc);
while (in.peekByte()) |byte| { while (in.peekByte()) |byte| {
if (std.ascii.isWhitespace(byte)) { if (std.ascii.isWhitespace(byte)) {
try expectStreamBytes(in, "\r\n"); try expectStreamBytes(in, "\r\n");
@@ -393,6 +406,7 @@ pub const Message = union(MessageType) {
fn readSubject(alloc: std.mem.Allocator, in: *std.Io.Reader) ![]const u8 { fn readSubject(alloc: std.mem.Allocator, in: *std.Io.Reader) ![]const u8 {
var subject_list: std.ArrayList(u8) = try .initCapacity(alloc, 1024); var subject_list: std.ArrayList(u8) = try .initCapacity(alloc, 1024);
errdefer subject_list.deinit(alloc);
// Handle the first character // Handle the first character
{ {