szn

Bug 358 — Auto window rename: invalid free + aliased allocations

358. Second automatic rename double-frees; OSC titles orphan the dupe

File: src/server/server.zig:2717-2722 (cf. window.zig:265-269, 469-478) Severity: CRITICAL Status: ✅ FIXED

Fix (applied)

renderToDisplayClient’s auto-rename now calls win.setName(proc_name) — copying into the inline name_buf, which is the only way Window.name is ever set (init, rename-window, OSC titles) — and keeps last_foreground_name as a separately owned dupe. No free of win.name, no aliasing.

Test: auto-rename: name stays in name_buf, cache never aliases it — bug #358 (two consecutive renames plus a later OSC title, asserting inline storage, no aliasing, and clean single free at deinit under testing.allocator).

Problem

Running e.g. vim then htop in an automatic-rename window triggers:

  1. win.allocator.free(win.name) — but when the name came from an OSC title callback, Window.name points into the embedded name_buf, not the heap. Freeing it is an invalid free of an interior struct pointer.
  2. win.name and win.last_foreground_name are set to the same duped slice. On the next rename both fields are freed → double free.
  3. Once name == duped, any later setName() (OSC title) repoints name at name_buf, permanently orphaning duped.

Masked today because these frees go through the session arena (no-op unless most-recent allocation); under any GPA this is corruption on the second rename.

Fix (proposed)

Never free win.name here (it is not owned by this code path — see the window.zig:574-579 test documenting “name is not owned”); dupe separately for last_foreground_name, freeing only the previous last_foreground_name if it is tracked as owned. Add an ownership comment on both fields plus a regression test simulating two consecutive renames followed by an OSC title change.