szn

Bug 398 — Window.init / addPane double-destroy the Pane when Pane.init fails

398. Window.init / addPane double-destroy the Pane when Pane.init fails: catch body and a live errdefer both destroy it (#239 incomplete fix)

Severity: HIGH Status: ✅ RESOLVED

var pane = try allocator.create(Pane);
errdefer allocator.destroy(pane);                     // :295
pane.* = Pane.init(allocator, 0, width, height) catch |err| {
    allocator.destroy(pane);                          // :297 explicit
    return err;                                       // -> :295 errdefer ALSO fires
};
errdefer { pane.deinit(); allocator.destroy(pane); }  // :300-303

Pane.init failure: catch-destroy + :295 errdefer = two destroys of the same struct. Any later fallible step (panes.append :311, Layout.init :314; in addPane: panes.append :406) fires both stacked errdefers (:295 and :300) = two destroys plus one Pane.deinit. Under GPA (tests) this is a hard double-free; against the session arena the free/destroy unwind is by allocation identity — a second pop corrupts the arena stack. The #239 regression test only fails allocation at index 0 (create itself), so neither double path is covered.

Fix: register the errdefer after the catch (layout.zig:115-123 shows the correct ordering) and drop the catch-destroy, or delete the first errdefer once the value is assigned.