szn

Bug 515 — pushOwned double-free on the OOM path

515. Two ownership layers both free the same slices when insert fails

Severity: MEDIUM Status: ✅ FIXED Files: src/buffer.zig:41-44, src/server/server.zig:1717-1723, src/server/server.zig:1834-1841, src/server/server.zig:2002-2010

Problem

Bug #456 reported that pushOwned leaked name and data when insert failed, and the fix added cleanup inside pushOwned:

// src/buffer.zig:39-46
pub fn pushOwned(self: *BufferList, name: []const u8, data: []const u8) !void {
    self.evictOldestIfFull();
    errdefer {
        self.allocator.free(name);
        self.allocator.free(data);
    }
    try self.items.insert(self.allocator, 0, .{ .name = name, .data = data });
}

But all three call sites already had their own cleanup for the same slices:

// src/server/server.zig:1717-1723 (yank in copy mode)
const data = cm.yankSelection(self.allocator, &pane.screen.grid) catch null;
if (data) |d| {
    errdefer self.allocator.free(d);
    const name = try self.buffers.generateName();
    errdefer self.allocator.free(name);
    try self.buffers.pushOwned(name, d);
}

On an insert OOM the sequence is:

  1. pushOwned’s errdefer frees name and d.
  2. The error propagates out of the if (data) |d| block.
  3. The block’s errdefers run in reverse and free name and d again.

Same allocator, same slices → double free. The server.zig:2370 comment (“pushOwned takes ownership and frees both slices on failure (bug #456)”) confirms the contract, which makes the caller-side errdefers the wrong half of the pair.

The identical pattern appears at the other two yank sites (copy-mode mouse release and non-copy-mode mouse release).

Fix

The three yank sites were identical, so the ownership rule now lives in one place instead of three:

// src/server/server.zig
fn yankToBuffer(buffers: *buffer_mod.BufferList, allocator: std.mem.Allocator, data: []const u8) !void {
    const name = buffers.generateName() catch |err| {
        allocator.free(data);
        return err;
    };
    try buffers.pushOwned(name, data);
}

and each call site is now just:

if (data) |d| {
    try yankToBuffer(&self.buffers, self.allocator, d);
}

Ownership of data transfers on entry: on success pushOwned has taken it, on failure it has been freed. The caller’s errdefers are gone, so nothing frees it twice.

Note that the guard on the generateName failure path is still required — that error happens before pushOwned takes ownership, so the slice would leak without it. Only the post-pushOwned frees were wrong.

Regression test

yankToBuffer frees the selection exactly once on any failure — bug #515 (src/server/server.zig) sweeps FailingAllocator.fail_index 0..7 so both the generateName and the pushOwned-insert failure paths run, and asserts the buffer list is empty on failure and holds exactly one entry on success. The pre-#515 shape — helper cleanup plus a caller errdefer — trips the testing allocator’s double-free detection here.

Verified with the full suite on Zig 0.16.0 and 0.17.0: 1048/1048 passing on both.

Bug #456 established that pushOwned takes ownership and frees both slices on failure; this bug is the caller half of that contract, which #456 did not update. Bug #457 was a sibling dead errdefer in the set-buffer path.