From 3dbd3bbcc7ba00ab4def14dadaf5335e7c566f1e Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 8 Nov 2026 20:54:12 -0300 Subject: fix: the ledger's recent.merge leak on an out-of-memory appendSlice is not one: merge's defer frees every path still in opens on that return; a test fails each of a merge's allocations in turn and testing.allocator finds nothing left, kept as the pin Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/recent.zig | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/recent.zig') diff --git a/src/recent.zig b/src/recent.zig index 31ed9c4c..5cccf18b 100644 --- a/src/recent.zig +++ b/src/recent.zig @@ -262,3 +262,21 @@ test "a merge puts this session's changes over the file's list, keeps what is op try std.testing.expect(l.get("/mine").?.open); try std.testing.expect(!l.get("/theirs").?.open); } + +test "a merge that runs out of memory at any allocation leaks nothing" { + var fail_index: usize = 0; + while (fail_index < 64) : (fail_index += 1) { + var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = std.math.maxInt(usize) }); + const gpa = failing.allocator(); + var l: List = .{}; + defer l.deinit(gpa); + l.opened(gpa, "/mine"); + l.opened(gpa, "/kept"); + l.closed(gpa, "/kept", 4, 2, 0, 0); + for (l.entries.items) |*e| e.changed = false; + // From here, the fail_index'th allocation fails. + failing.fail_index = failing.alloc_index + fail_index; + l.merge(gpa, "0 0 0 0 /theirs\n9 1 0 0 /kept\n"); + if (!failing.has_induced_failure) break; + } +} -- cgit v1.3