szn

Bug 562 — destroyPane test reads a freed Pane

562. A regression test dereferences a pane after the call that frees it

Severity: MEDIUM (test-only; crashes the whole suite on Zig 0.17)
Status: ✅ RESOLVED
Files: src/server/server.zig:4723-4745

Symptom

On a clean cache the suite passes on Zig 0.16.0 but crashes on 0.17.0:

$ rm -rf .cache .zig-cache && /opt/zig/0.17.0/zig build test -Doptimize=ReleaseFast --summary all
error: 'server.server.test.destroyPane deinitializes the last pane of a window' terminated with signal SEGV
+- run test 1081 pass, 1 crash (1082 total)

Cause

server.destroyPane(pane1);
try testing.expect(pane1.deinited);   // pane1 has already been freed

pane1 is the only pane of its window, so Server.destroyPane takes the win.panes.items.len <= 1 branch and calls Session.killWindow. That runs Window.deinit → Layout.deinit, which calls pane.deinit() and then allocator.destroy(pane). The window itself is destroyed too. The production path is correct: destroyPane never touches pane or win after killWindow. Only the test reads freed memory.

Why only Zig 0.17 crashes

  Zig 0.16.0 Zig 0.17.0
std.testing.allocator DebugAllocator SafeAllocator (test runner sets check_write_after_free = true)
Freed memory stays mapped; stale bytes still readable never reused; pages returned to page_allocator
pane1.deinited reads the stale true written by deinit just before the free, so the test passes by luck reads unmapped memory → SIGSEGV

Fix

The test no longer dereferences pane1 or w1 after destroyPane. It checks the observable outcome, comparing pointer values only:

server.destroyPane(pane1);

try testing.expectEqual(@as(usize, 1), server.sessions.items.len);
try testing.expectEqual(@as(usize, 1), s.windows.items.len);
try testing.expect(s.windows.items[0] != w1);
try testing.expect(s.active_window == s.windows.items[0]);

The pane is still proven to be deinitialized: if Pane.deinit were skipped, its Screen/grid buffers would leak and testing.allocator’s leak check would fail the test.

Lesson

Under Zig 0.17, a use-after-free in a test turns from silent luck into a crash. Never read a pointer after the call that frees it, even to assert it was freed. Check the owner’s state instead, and let the leak checker prove the deinit happened.

Verification

rm -rf .cache .zig-cache followed by zig build test -Doptimize=ReleaseFast gives 1082/1082 passing on both Zig 0.16.0 and 0.17.0. zig fmt --check is clean.