diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-23 17:47:04 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-23 17:50:13 -0300 |
| commit | fcc08ec6ceb9af11aeda8489cef0d4085878ef48 (patch) | |
| tree | 81eee9825ad2ed222c25bc9a35958f71e2d00a82 /core | |
| parent | 6de26a20a5fb547f3d032f6ae2062066f356a659 (diff) | |
| download | pardes-fcc08ec6ceb9af11aeda8489cef0d4085878ef48.tar.gz pardes-fcc08ec6ceb9af11aeda8489cef0d4085878ef48.zip | |
theme files: one nested deeper than 64 levels is refused in words before std's ZON parser sees it: round 1 inferred a stack overflow from core/colors.zig `loadThemeFile` (std.zon.parse.fromSlice, whose std.zig parser recurses a level a bracket with no bound); RUN (the test below on the base, Debug, a 16 MiB stack): 10 and 1 000 levels refused in words (a type error, with its line), 2 000 too, 5 000 a SEGMENTATION FAULT in std/zig/Parse.zig (parseTypeExpr, parsePrimaryTypeExpr: the stack), so a theme file of some 10 KB of `.{` ended the session, and every session after it whose config loads that theme at its start; a user hits it only with a file made so; the theme file is the one ZON from outside the program (the config file is lines of words); now `nesting` counts how deep its brackets go with std's own tokenizer (iterative: strings and comments are tokens of their own, nothing is parsed twice), and past `max_theme_nesting` (64; the schema is three deep) the file is refused as any bad theme file is, its pane told "theme file <path>: nested past 64 levels" and the theme in effect kept; test: core/colors.zig "a theme file nested 10, 1 000 and 100 000 deep is refused in words, never a stack overflow"; core-test Debug theme tests 26/27 (1 skipped)
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core')
| -rw-r--r-- | core/colors.zig | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/core/colors.zig b/core/colors.zig index 7e692fe5..5579ed05 100644 --- a/core/colors.zig +++ b/core/colors.zig @@ -687,6 +687,29 @@ fn revertThemeFile(p: *Pardes) void { p.emit(.{ .theme_file = .{ .generation = generation, .on = loaded.len > 0 } }); } +/// The deepest a theme file's brackets nest: its schema is three levels +/// deep, and std's ZON parser recurses a level a bracket with no bound of +/// its own (a file 5 000 deep overflowed a 16 MiB stack in a Debug build: +/// the session's end, at every start that loaded it). +const max_theme_nesting = 64; + +/// How deep `source`'s brackets nest, as std's tokenizer reads it (strings +/// and comments are tokens of their own). +fn nesting(source: [:0]const u8) usize { + var tokens: std.zig.Tokenizer = .init(source); + var depth: usize = 0; + var most: usize = 0; + while (true) switch (tokens.next().tag) { + .eof => return most, + .l_brace, .l_bracket, .l_paren => { + depth += 1; + most = @max(most, depth); + }, + .r_brace, .r_bracket, .r_paren => depth -|= 1, + else => {}, + }; +} + pub fn loadThemeFile(p: *Pardes, generation: u32, bytes: []const u8) bool { // ZON's generated field dispatch grows with the complete theme schema. @setEvalBranchQuota(10000); @@ -700,6 +723,10 @@ pub fn loadThemeFile(p: *Pardes, generation: u32, bytes: []const u8) bool { return false; }; defer p.gpa.free(source); + if (nesting(source) > max_theme_nesting) { + themeFileFailure(p, request, std.fmt.comptimePrint("nested past {d} levels", .{max_theme_nesting})); + return false; + } // The parse's results live in `arena`; the theme kept owns only its name. var arena: std.heap.ArenaAllocator = .init(p.gpa); defer arena.deinit(); @@ -931,6 +958,31 @@ test "a theme file that cannot be read is named in the failure" { try std.testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "theme file /nowhere/mine.zon: ") != null); } +test "a theme file nested 10, 1 000 and 100 000 deep is refused in words, never a stack overflow" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .config_dir = "/tmp/pardes-config" }); + defer p.deinit(); + for ([_]usize{ 10, 1000, 100_000 }) |depth| { + requestThemeFile(p, 0, "themes/deep.zon"); + var source: std.ArrayList(u8) = .empty; + defer source.deinit(gpa); + try source.appendSlice(gpa, ".{ .name = \"deep\", .bg = "); + for (0..depth) |_| try source.appendSlice(gpa, ".{"); + try source.appendSlice(gpa, "1"); + for (0..depth) |_| try source.append(gpa, '}'); + try source.appendSlice(gpa, " }\n"); + p.panes[0].?.msg_len = 0; + // Up to 2026-11-23 std's parser took it: 5 000 deep overflowed the + // stack (a Debug build's 16 MiB). + try std.testing.expect(!loadThemeFile(p, p.theme_file_generation, source.items)); + const said = p.panes[0].?.msg[0..p.panes[0].?.msg_len]; + std.testing.expect(std.mem.indexOf(u8, said, "theme file /tmp/pardes-config/themes/deep.zon: ") != null) catch |err| { + std.debug.print("{d} deep: said {s}\n", .{ depth, said }); + return err; + }; + } +} + test "ThemeFile parses ZON atomically and Theme returns to the compiled ring" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, |
