Skip to content

Commit e7843ed

Browse files
committed
chore: apply suggested fixes
1 parent 79943da commit e7843ed

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

src/msgpack.zig

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -882,23 +882,18 @@ fn clonePayload(payload: Payload, allocator: Allocator) !Payload {
882882

883883
.map => |m| {
884884
var new_map = Map.init(allocator);
885-
errdefer new_map.deinit();
885+
errdefer (Payload{ .map = new_map }).free(allocator);
886886

887887
// Clone all entries
888888
var it = m.map.iterator();
889889
while (it.next()) |entry| {
890-
var cloned_key: ?Payload = null;
891-
var cloned_value: ?Payload = null;
892-
errdefer {
893-
if (cloned_value) |v| v.free(allocator);
894-
if (cloned_key) |k| k.free(allocator);
895-
}
896-
897-
cloned_key = try clonePayload(entry.key_ptr.*, allocator);
898-
cloned_value = try clonePayload(entry.value_ptr.*, allocator);
890+
const cloned_key = try clonePayload(entry.key_ptr.*, allocator);
891+
errdefer cloned_key.free(allocator);
892+
const cloned_value = try clonePayload(entry.value_ptr.*, allocator);
893+
errdefer cloned_value.free(allocator);
899894

900895
// Use putInternal to insert without additional cloning
901-
try new_map.putInternal(cloned_key.?, cloned_value.?);
896+
try new_map.putInternal(cloned_key, cloned_value);
902897
}
903898
return Payload{ .map = new_map };
904899
},

src/test.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3632,6 +3632,7 @@ test "clonePayload arr partial fail path frees partially cloned elements" {
36323632
try std.testing.expect(err == error.OutOfMemory);
36333633
}
36343634

3635+
// Verify post-failure allocator state is valid: next alloc must succeed.
36353636
const next_alloc = try clone_alloc.alloc(u8, 16);
36363637
clone_alloc.free(next_alloc);
36373638
}
@@ -3655,6 +3656,7 @@ test "clonePayload map partial fail path frees partially cloned entries" {
36553656
try std.testing.expect(err == error.OutOfMemory);
36563657
}
36573658

3659+
// Verify post-failure allocator state is valid: next alloc must succeed.
36583660
const next_alloc = try clone_alloc.alloc(u8, 16);
36593661
clone_alloc.free(next_alloc);
36603662
}

0 commit comments

Comments
 (0)