From 26b52707c97b98f650a3402654d27b4a62eed8f8 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 24 Nov 2026 22:32:26 -0300 Subject: dump: a load frees what it read after the core as the things it made, once each (item 5 of A2, on line A2; the fix B made in ozlumvqo, the same hunks): the 40,000-seed dump fuzz on xqqlroux panicked at join seed 19224 with `double free` (the first free dump.zig:422 `defer gpa.free(theme)` in loadKnowing, the second the errdefer's rollback, the slice allocated by walk.zig slice:1847 inside the core's own fields); THE CAUSE: walk.Load.ref hands back a slice made before (the load's `shared` list), which for a damaged dump's theme read after the core is one the loaded core holds, and loadKnowing freed its trailing reads (mounts, theme, the other views' shares) by hand: a reference freed once by the defer and again by the rollback of a later failed read; THE FIX (B's, ozlumvqo, verbatim, so the merge coincides): one defer frees `l.shared.items[read..]` (what the load made after the core) and drops it from the rollback, the views' shares the same; a user could hit it only with a damaged or hostile dump (a join's stream, a Restore file); TEST FIRST: fuzz.zig's session now holds a view in Help (`SPC t w` remembered, `SPC ? hel`: View.tag_state out of rest in every dump it cuts) and checks join seed 19224 by name (fails before: double free); the fuzz at 40,000 join and 1,779 image seeds green (core-test filter, 2 min, 8 of 8) Co-Authored-By: Claude Opus 5.5 --- core/dump.zig | 19 +++++++++---------- core/fuzz.zig | 7 +++++++ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/core/dump.zig b/core/dump.zig index 49b2605a..4bb89dae 100644 --- a/core/dump.zig +++ b/core/dump.zig @@ -406,20 +406,19 @@ fn loadKnowing(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, notes: l.allocs = .{ .image = p.image_gpa, .terminal = terminal_gpa }; try l.into(Pardes, p); const read = l.shared.items.len; - const mounts = try l.get([]const filesystem.Mount); + // What is read after the core (its mounts, its theme) is freed here, + // what the load made for it once each, and no rollback of it: a + // damaged dump's reference to a slice the core holds is the core's, + // never freed here (a theme that was one was freed by hand and again + // by the rollback). defer { - for (mounts) |m| { - gpa.free(m.name); - gpa.free(m.dial); - } - gpa.free(mounts); - // Freed here: no rollback of them. + for (l.shared.items[read..]) |m| if (m.bytes.len > 0) m.gpa.rawFree(m.bytes, m.alignment, @returnAddress()); l.shared.shrinkRetainingCapacity(read); } + const mounts = try l.get([]const filesystem.Mount); const was = try l.get(OnDisk); if (on_disk) |out| out.* = was; const theme = try l.get([]const u8); - defer gpa.free(theme); // This build's place for that theme; one it has not, its first. p.settings.theme = for (colors.themes, 0..) |t, i| { if (std.mem.eql(u8, t.name, theme)) break i; @@ -453,8 +452,8 @@ fn loadKnowing(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, notes: else => return err, }; views.undumped(p, others); - for (others) |o| gpa.free(o.panes); - gpa.free(others); + // What it made, as the mounts' (above). + for (l.shared.items[at..]) |m| if (m.bytes.len > 0) m.gpa.rawFree(m.bytes, m.alignment, @returnAddress()); l.shared.shrinkRetainingCapacity(at); afterLoad(p); p.readSessionFacts(); diff --git a/core/fuzz.zig b/core/fuzz.zig index 616636ab..f83e5c79 100644 --- a/core/fuzz.zig +++ b/core/fuzz.zig @@ -777,6 +777,10 @@ test "a dump and an image cut, flipped and grown: refused or loaded, never a pan const gpa = testing.allocator; const p = try th.withFile(gpa, "alpha beta\ngamma\n\tdelta \xe6\x97\xa5\n"); defer p.deinit(); + // A view's tag out of rest in it (View.tag_state): SPC t w remembered + // (a group's last leaf), then Help typed into (a scope, a field, hits). + for (" tw ?hel") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + try testing.expect(p.view().tag_state.kind == .help and p.view().tag_state.nhits > 0); // The byte dump a join streams, and the image a Dump keeps. const bytes = try pardes.dump.byteDump(p); defer gpa.free(bytes); @@ -785,6 +789,9 @@ test "a dump and an image cut, flipped and grown: refused or loaded, never a pan // Seconds a road, each core used (`use`). The image's past 1779 seeds // reach xitdb's own reader (a slot's tag past its kinds: a `switch` on // it), a dependency's. + // Join seed 19224: the theme read after the core a reference to a + // slice the core holds, freed by hand and again by the rollback. + try loadSeed(bytes, .join, 19224); for ([_][]const u8{ bytes, image }, [_]Road{ .join, .restore }, [_]u64{ 2048, 1024 }) |whole, road, n| { try loadSeed(whole, road, 0); for (1..n + 1) |seed| try loadSeed(whole, road, seed); -- cgit v1.3 From d266ff2b816163ec9d09ec7ab79955b8fce719b0 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 24 Nov 2026 22:32:26 -0300 Subject: mouse: a click that gives the keyboard to another pane ends a tag's level or typing state as Esc does (item 6 of A2, the coordinator's ruling on SPEC §3.4.6: only the pane with the keyboard leaves rest): a level (SPC's, a helix prefix's once shown, C-w's), Help or describe-key stayed out of rest when a click moved the keyboard, and followed it into the other pane's tag (the leader case had to press Esc after its struck key for that); now handleMouse notes the pane out of rest (TagState.hostOf) and, when the event leaves the keyboard on another pane, puts that pane's tag at rest as Esc does (TagState.toRest on it: its text's prefix too; the pane the click went to keeps its own); a click in the band or in the level's own tag keeps it; TEST FIRST: TagState.zig "a click that gives the keyboard to another pane ends a level as Esc does; one in the level's own tag keeps it" (failed before at the click into the other pane's body: the leader still current) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- core/TagState.zig | 42 ++++++++++++++++++++++++++++++++++++++++++ core/mouse.zig | 11 +++++++++++ 2 files changed, 53 insertions(+) diff --git a/core/TagState.zig b/core/TagState.zig index 191aad8d..fb71ce77 100644 --- a/core/TagState.zig +++ b/core/TagState.zig @@ -933,3 +933,45 @@ test "a dump's tag state that indexes past what it names is at rest when loaded, loaded(p); try std.testing.expect(s.kind == .rest); } + +test "a click that gives the keyboard to another pane ends a level as Esc does; one in the level's own tag keeps it" { + const config = @import("config.zig"); + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); + defer p.deinit(); + _ = try p.setTestFile("one\ntwo\n"); + const top = p.view().active; + p.newScratchBelow(top); + const low = p.view().active; + try std.testing.expect(low != top); + const click = struct { + fn at(q: *Pardes, col: u16, row: u16) void { + q.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = col, .row = row } }); + q.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = col, .row = row } }); + } + }.at; + const rt = p.view().rects[top]; + const rl = p.view().rects[low]; + // SPC's level in the lower pane's tag; a click in its own tag keeps it. + p.update(.{ .key = .{ .cp = ' ' } }); + try std.testing.expect(hostOf(p) == low); + click(p, rl.x + config.GUTTER + 2, rl.y); + try std.testing.expect(hostOf(p) == low and p.view().prefix == .leader); + // A click in the other pane's body: the keyboard goes there, the level + // ends, nothing out of rest. + click(p, rt.x + config.GUTTER + 1, rt.y + pardes.BOX_H); + try std.testing.expectEqual(top, p.view().active); + try std.testing.expect(hostOf(p) == null and p.view().prefix == .none and p.view().tag_state.root == null); + // A helix prefix's level (g, shown after its delay) the same, its text's + // prefix gone with it; Help, a typing state, the same. + p.update(.{ .key = .{ .cp = 'g' } }); + p.advance(p.clock.now_ns); + p.clock.now_ns = p.view().tag_state.due_ns; + try std.testing.expect(hostOf(p) == top); + click(p, rl.x + config.GUTTER + 1, rl.y + pardes.BOX_H); + try std.testing.expectEqual(low, p.view().active); + try std.testing.expect(hostOf(p) == null and p.panes[top].?.body.normal.prefix == null); + for (" ?") |c| p.update(.{ .key = .{ .cp = c } }); + try std.testing.expect(p.view().tag_state.kind == .help); + click(p, rt.x + config.GUTTER + 1, rt.y + pardes.BOX_H); + try std.testing.expect(p.view().tag_state.kind == .rest and hostOf(p) == null); +} diff --git a/core/mouse.zig b/core/mouse.zig index 7a918883..3ed4ce48 100644 --- a/core/mouse.zig +++ b/core/mouse.zig @@ -499,6 +499,17 @@ fn chordCutPaste(p: *Pardes, cut: bool) edit.Error!void { pub fn handleMouse(p: *Pardes, event: Mouse) void { const v = p.view(); + // A click that gives the keyboard to another pane ends the level or + // typing state of the tag out of rest as Esc does, its text's prefix + // with it (SPEC §3.4.6: only the pane with the keyboard leaves rest); + // one in its band or its own tag keeps it. + const host = pardes.TagState.hostOf(p); + defer if (host) |was| if (v.active != was) { + const to = v.active; + v.active = was; + pardes.TagState.toRest(p); + v.active = to; + }; var m = event; // A click goes somewhere else: the question it leaves unanswered is // taken back, and the pane stays. A wheel only looks around. -- cgit v1.3