diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-23 17:26:17 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-23 17:47:03 -0300 |
| commit | 6de26a20a5fb547f3d032f6ae2062066f356a659 (patch) | |
| tree | d732376e0f31260a1cf57660f7a534a144dec002 | |
| parent | 2f3e7717dfa61421c71475130f540f9db6f7478c (diff) | |
| download | pardes-6de26a20a5fb547f3d032f6ae2062066f356a659.tar.gz pardes-6de26a20a5fb547f3d032f6ae2062066f356a659.zip | |
numbers from outside are checked where they are read: a diff's hunk header, a place's line and column, a PDF's page size (the failing runs first, kept in the scratch): core/fuzz.zig gains two fuzzes, "a diff's hunk headers with numbers past any file's: drawn, formatted and looked at, never a panic" (each seed a diff whose `@@` numbers are drawn from around and past every int width, drawn as a `.diff`, formatted as the patch view, looked at on every row) and "a language server's rows and a typed Look with numbers past any file's: landed, stepped and drawn, never a panic" (rows as lsp.sourceSpanRow writes them for seven kinds of answer, landed, stepped with n N j k Enter, drawn; the same places typed to Look in its three spellings); on the base the first panicked at core/diff.zig:134 (`w.new_at += 1`, integer overflow: a `.diff`, or a `cat` of one in a terminal, with `@@ -1 +18446744073709551615 @@` killed the session and every client that drew it) and the second at core/look.zig:751 (`@intCast(at.col - 1)`, integer does not fit: `Look /test.txt:1:3000000000` typed, or such text B3'd anywhere, a server's row with a column past i32 looked at); the language server's own rows land through Query's landing, which clamped already (its client validates each coordinate to a u32); FIXES: core/diff.zig `number` reads a header's number as a u32 (a line or a count of lines: no file pardes opens has more), one past it makes the row no header, drawn as text as patch(1) refuses it, so the walk's counts, hunks.zig `moved`'s i64 and jj.zig's u32 casts all hold; core/look.zig `focusPaneLine` takes a line past an i32 as no line and lands a column past its line's end at the end (`columnOn`, as a search's landing and Text.clampCursor have it: `Look x:2:9` on a 5-byte line lands at its end, where it stood past it before; a terminal's column, a jump's, as it was), the newly opened file's the same, and `selectSpan` saturates each number into cellRange, which clamps to the text; the pdf addon: a page's size is held where the bridge reads it (addons/pdf/pdf_bridge.c `pardes_pdf_get_page_size`, every size the layout sees comes through it) to `PARDES_PDF_MAX_PAGE_POINTS` (2^24 pt, 5.9 km, on a side; MuPDF bounds neither a MediaBox nor a UserUnit, nor a float's infinity) and the addon says so ("a page past 16777216 pt on a side: laid out at that size") when one lands: on the base a page 1e9 pt wide panicked the addon's paint (`@intFromFloat`, out of bounds, pdf.zig:413: the session and every client that drew it), test/pdf-fixtures/huge-pages.pdf (a 1e9 pt side, a side past a float, a UserUnit past a float, a page as any) opened, drawn, stepped and ended in the addon's script (addons/pdf/pdf.script, pdf.out); a test in core/look.zig of the landing itself; core-test Debug 950/1116 (166 skipped; base 947/1113), shell-test Debug 1162/1165 (3 skipped), addon-test Debug whole
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
| -rw-r--r-- | addons/pdf/pdf.out | 33 | ||||
| -rw-r--r-- | addons/pdf/pdf.script | 8 | ||||
| -rw-r--r-- | addons/pdf/pdf.zig | 9 | ||||
| -rw-r--r-- | addons/pdf/pdf_bridge.c | 2 | ||||
| -rw-r--r-- | addons/pdf/pdf_bridge.h | 7 | ||||
| -rw-r--r-- | core/diff.zig | 7 | ||||
| -rw-r--r-- | core/fuzz.zig | 98 | ||||
| -rw-r--r-- | core/look.zig | 40 | ||||
| -rw-r--r-- | test/pdf-fixtures/huge-pages.pdf | 61 |
9 files changed, 256 insertions, 9 deletions
diff --git a/addons/pdf/pdf.out b/addons/pdf/pdf.out index 49ba2a88..2d4566b1 100644 --- a/addons/pdf/pdf.out +++ b/addons/pdf/pdf.out @@ -201,3 +201,36 @@ tag [3/16] petscii:on palette:commodore ascii:on docs/design.pdf |the native macOS host watches with debounced DispatchSourc |instead, then restats the exact path under a pane-generati |guard. All three macOS watchers mark the same two things f +> open test/pdf-fixtures/huge-pages.pdf +window 58x1 +tag [1/4] petscii:on palette:commodore ascii:on test/pdf-fixtures/huge-pages.pdf +| a page past 16777216 pt on a side: laid out at that size +| +> graphics on +tag [1/4] petscii:off palette:commodore ascii:on test/pdf-fixtures/huge-pages.pdf +| a page past 16777216 pt on a side: laid out at that size +| +placement 1x1 src 0,0 1x1 over 0,1 58x1 key fb85a0e4117bdb75 at 0,0 464x1 +placement 1200x1200 src 0,0 1200x19 over 0,1 58x1 key a44f40e9b170318e at 0,9 464x7 +> size 60 20 +window 58x1 +tag [1/4] petscii:off palette:commodore ascii:on test/pdf-fixtures/huge-pages.pdf +| a page past 16777216 pt on a side: laid out at that size +| +placement 1x1 src 0,0 1x1 over 0,1 58x1 key fb85a0e4117bdb75 at 0,0 464x1 +placement 1200x1200 src 0,0 1200x19 over 0,1 58x1 key a44f40e9b170318e at 0,9 464x7 +> key j +tag [2/4] petscii:off palette:commodore ascii:on test/pdf-fixtures/huge-pages.pdf +| a page past 16777216 pt on a side: laid out at that size +| +placement 1200x1200 src 0,18 1200x42 over 0,1 58x1 key a44f40e9b170318e at 0,0 464x16 +> key 2j +tag [2/4] petscii:off palette:commodore ascii:on test/pdf-fixtures/huge-pages.pdf +| a page past 16777216 pt on a side: laid out at that size +| +placement 1200x1200 src 0,100 1200x43 over 0,1 58x1 key a44f40e9b170318e at 0,0 464x16 +> key G +tag [4/4] petscii:off palette:commodore ascii:on test/pdf-fixtures/huge-pages.pdf +| a page past 16777216 pt on a side: laid out at that size +| +placement 816x64 src 0,0 816x29 over 0,1 58x1 key aa6de78f475e58f7 at 0,0 464x16 diff --git a/addons/pdf/pdf.script b/addons/pdf/pdf.script index 60e4bfca..62629ded 100644 --- a/addons/pdf/pdf.script +++ b/addons/pdf/pdf.script @@ -23,3 +23,11 @@ key 3gj key gk key 999gj key gj +# pages past any size (a side of 1e9 pt, one past a float, a UserUnit +# past a float): laid out and drawn at the bridge's bound, never a panic +open test/pdf-fixtures/huge-pages.pdf +graphics on +size 60 20 +key j +key 2j +key G diff --git a/addons/pdf/pdf.zig b/addons/pdf/pdf.zig index 79d79219..5694e8c8 100644 --- a/addons/pdf/pdf.zig +++ b/addons/pdf/pdf.zig @@ -270,6 +270,7 @@ pub const Pdf = extern struct { return addon.say("not a PDF pardes can read", .err); } o.d = d; + sayHuge(o.size[0..1]); st.pages = @intCast(pages); // every page the first's size until a lane measures them o.measured = st.pages == 1; @@ -812,6 +813,7 @@ pub const Pdf = extern struct { if (res.n != st.pages or sizes.len != st.pages) return; for (o.size, sizes) |*to, from| to.* = from; o.measured = true; + sayHuge(o.size); // laid again where a page is not the first's size for (o.size) |s| if (!std.meta.eql(s, o.size[0])) { for (&st.views) |*l| l.laid = false; @@ -958,6 +960,13 @@ fn ownOf(st: *const Pdf) *Own { } /// Page `p`'s size as the strip is laid with it. +/// A page the bridge held to its bound (a crafted MediaBox) is said. +fn sayHuge(sizes: []const m.pardes_pdf_page_size) void { + const most: f32 = m.PARDES_PDF_MAX_PAGE_POINTS; + for (sizes) |s| if (s.width >= most or s.height >= most) + return addon.say(std.fmt.comptimePrint("a page past {d} pt on a side: laid out at that size", .{m.PARDES_PDF_MAX_PAGE_POINTS}), .note); +} + fn sizeOf(st: *const Pdf, o: *Own, p: usize) m.pardes_pdf_page_size { if (st.sizes != .measured) return o.size[0]; // A client joined after the lane measured them: measured here. diff --git a/addons/pdf/pdf_bridge.c b/addons/pdf/pdf_bridge.c index 3b326655..e1279f51 100644 --- a/addons/pdf/pdf_bridge.c +++ b/addons/pdf/pdf_bridge.c @@ -591,6 +591,8 @@ pardes_pdf_get_page_size( out->height = bounds.y1 - bounds.y0; if (!(out->width > 0.0f) || !(out->height > 0.0f)) fz_throw(ctx, FZ_ERROR_FORMAT, "PDF page has empty bounds"); + out->width = fminf(out->width, PARDES_PDF_MAX_PAGE_POINTS); + out->height = fminf(out->height, PARDES_PDF_MAX_PAGE_POINTS); } fz_catch(ctx) { diff --git a/addons/pdf/pdf_bridge.h b/addons/pdf/pdf_bridge.h index fd5d05d0..0e52c505 100644 --- a/addons/pdf/pdf_bridge.h +++ b/addons/pdf/pdf_bridge.h @@ -203,7 +203,12 @@ enum { PARDES_PDF_MAX_LINK_URI_BYTES = 64 * 1024, /* No page raster is taller than this, whatever it is shown at. */ - PARDES_PDF_MAX_RASTER_ROWS = 1 << 20 + PARDES_PDF_MAX_RASTER_ROWS = 1 << 20, + + /* No page is laid out wider or taller than this, in points (5.9 km): a + * crafted MediaBox or UserUnit has any size, a float's infinity too, and + * MuPDF bounds neither; pdf.zig counts a page's pixels in u32s. */ + PARDES_PDF_MAX_PAGE_POINTS = 1 << 24 }; pardes_pdf_document *pardes_pdf_open_memory(const unsigned char *bytes, size_t length, int *page_count); diff --git a/core/diff.zig b/core/diff.zig index 659576d3..edf037fe 100644 --- a/core/diff.zig +++ b/core/diff.zig @@ -294,11 +294,14 @@ fn range(line: []const u8, at: *usize) ?[2]usize { return .{ start, 1 }; } +/// A header's number: a line of a file, or a count of them, so a u32 (no +/// file pardes opens has more lines); one past it makes the line no header, +/// where the walk's count would overflow (a crafted `+18446744073709551615`). fn number(line: []const u8, at: *usize) ?usize { const from = at.*; - var n: usize = 0; + var n: u32 = 0; while (at.* < line.len and std.ascii.isDigit(line[at.*])) : (at.* += 1) - n = std.math.add(usize, std.math.mul(usize, n, 10) catch return null, line[at.*] - '0') catch return null; + n = std.math.add(u32, std.math.mul(u32, n, 10) catch return null, line[at.*] - '0') catch return null; return if (at.* > from) n else null; } diff --git a/core/fuzz.zig b/core/fuzz.zig index f324a48e..23d007ac 100644 --- a/core/fuzz.zig +++ b/core/fuzz.zig @@ -771,3 +771,101 @@ test "9P: a rows style past the syntax's kinds is refused, never drawn" { _ = try p.render(p.frame_arena.allocator()); } } + +// --------------------------------------------------------------------------- +// (v, vi) Numbers in a file's text and in another program's answer: a +// diff's hunk headers (a `.diff` drawn, the patch view, a look's target) +// and a language server's rows (each kind's answer landed, stepped, drawn; +// the same places typed to Look). + +const diff = @import("diff.zig"); +const hunks = @import("hunks.zig"); +const lsp = pardes.lsp; + +/// Around what an int of each width holds, and past it. +const numbers = [_][]const u8{ "0", "1", "2", "3", "5", "65535", "2147483647", "2147483648", "3000000000", "4294967295", "4294967296", "9223372036854775807", "9223372036854775808", "18446744073709551615", "18446744073709551616", "99999999999999999999999" }; + +fn draw(p: *Pardes) !void { + p.sync(); + _ = p.frame_arena.reset(.retain_capacity); + _ = try p.render(p.frame_arena.allocator()); +} + +fn diffSeed(gpa: std.mem.Allocator, seed: u64) !void { + var prng: Random.DefaultPrng = .init(seed); + const r = prng.random(); + var text: std.ArrayList(u8) = .empty; + defer text.deinit(gpa); + try text.appendSlice(gpa, "diff --git a/x.zig b/x.zig\n--- a/x.zig\n+++ b/x.zig\n"); + for (0..1 + r.uintLessThan(usize, 3)) |_| { + try text.print(gpa, "@@ -{s}", .{pick(r, numbers)}); + if (chance(r, 0.7)) try text.print(gpa, ",{s}", .{pick(r, numbers)}); + try text.print(gpa, " +{s}", .{pick(r, numbers)}); + if (chance(r, 0.7)) try text.print(gpa, ",{s}", .{pick(r, numbers)}); + try text.appendSlice(gpa, " @@ fn x()\n"); + const lines = [_][]const u8{ " const a = 1;\n", "-const b = 2;\n", "+const b = 3;\n", "\\ No newline at end of file\n", "\n", "--- y\n", "+++ y\n", "text\n" }; + for (0..r.uintLessThan(usize, 9)) |_| try text.appendSlice(gpa, pick(r, lines)); + } + // A `.diff` drawn. + const p = try th.withFile(gpa, text.items); + defer p.deinit(); + const pane = p.panes[p.view().active].?; + gpa.free(pane.file.?.path); + pane.file.?.path = try gpa.dupe(u8, "/change.diff"); + pane.file.?.syntax_dirty = true; + try draw(p); + // The patch view. + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const ctx: hunks.Ctx = .{ .arena = arena.allocator() }; + _ = hunks.format(&ctx, "", text.items) catch {}; + // A look's target, at every row. + var rows: std.ArrayList([]const u8) = .empty; + defer rows.deinit(gpa); + var it = std.mem.splitScalar(u8, text.items, '\n'); + while (it.next()) |row| try rows.append(gpa, row); + for (0..rows.items.len) |row| _ = diff.targetAt(rows.items, row, false); +} + +test "a diff's hunk headers with numbers past any file's: drawn, formatted and looked at, never a panic" { + for (seeds) |seed| for (0..4) |k| try diffSeed(testing.allocator, seed *% 31 +% k); +} + +fn lspSeed(gpa: std.mem.Allocator, seed: u64) !void { + var prng: Random.DefaultPrng = .init(seed); + const r = prng.random(); + const p = try th.withFile(gpa, "alpha one\n\tbeta\n\t\tgamma \xe6\x97\xa5\n\n"); + defer p.deinit(); + var rows: std.ArrayList(u8) = .empty; + defer rows.deinit(gpa); + for (0..1 + r.uintLessThan(usize, 4)) |_| { + // As lsp.sourceSpanRow writes them; the file's own name. + try rows.print(gpa, "/test.txt:{s}:{s}", .{ pick(r, numbers), pick(r, numbers) }); + if (chance(r, 0.5)) try rows.print(gpa, "-{s}", .{pick(r, numbers)}); + try rows.appendSlice(gpa, " alpha\n"); + } + const kinds = [_]lsp.Kind{ .definition, .declaration, .implementation, .references, .diagnostics, .document_symbols, .workspace_symbols }; + const kind = pick(r, kinds); + if (lsp.supports.contains(kind)) { + p.lspRequest(0, kind, ""); + while (p.nextEffect()) |_| {} + if (p.lsp_ask.wait) |wait| p.update(.{ .lsp_resp = .{ .id = wait.id, .rows = rows.items } }); + try draw(p); + for ("nNjk\r") |key| { + p.update(.{ .key = .{ .cp = if (key == '\r') pardes.Key.enter else key } }); + try draw(p); + } + } + // The same places typed. + var look: [96]u8 = undefined; + p.update(.{ .command = try std.fmt.bufPrint(&look, "Look /test.txt:{s}:{s}", .{ pick(r, numbers), pick(r, numbers) }) }); + try draw(p); + p.update(.{ .command = try std.fmt.bufPrint(&look, "Look /test.txt:{s}:{s}-{s}:{s}", .{ pick(r, numbers), pick(r, numbers), pick(r, numbers), pick(r, numbers) }) }); + try draw(p); + p.update(.{ .command = try std.fmt.bufPrint(&look, "Look /test.txt:{s}-{s}", .{ pick(r, numbers), pick(r, numbers) }) }); + try draw(p); +} + +test "a language server's rows and a typed Look with numbers past any file's: landed, stepped and drawn, never a panic" { + for (seeds) |seed| for (0..4) |k| try lspSeed(testing.allocator, seed *% 31 +% k); +} diff --git a/core/look.zig b/core/look.zig index 53c26943..41b48393 100644 --- a/core/look.zig +++ b/core/look.zig @@ -747,20 +747,29 @@ pub fn focusPaneLine(p: *Pardes, id: usize, at: Spot, landing: enum { center, ke } } } - pane.body.cur_row = @intCast(at.line - 1); - pane.body.cur_col = if (at.col > 0) @intCast(at.col - 1) else 0; + pane.body.cur_row = std.math.cast(i32, at.line - 1) orelse return; + pane.body.cur_col = columnOn(pane, pane.body.cur_row, at.col); pane.body.cur_pinned = true; if (at.end_line != 0) return selectSpan(p, pane, at); pane.ensureCursorVisible(p.settings.tab); } +/// A place's column (from 1) as a cursor's on `row`: past the line's end (a +/// server's answer, a typed number: any) it is the end, as a search's +/// landing and clampCursor have it. A terminal's (a jump's) as it was. +fn columnOn(pane: *const Pane, row: i32, col: usize) i32 { + if (pane.file == null) return std.math.lossyCast(i32, col -| 1); + return @intCast(@min(col -| 1, panes.File.sourceLine(pane, row).len)); +} + fn selectSpan(p: *Pardes, pane: *Pane, at: Spot) void { const text = edit.flatSurface(p.editor(), &pane.body) catch |err| return p.reportError(.at(p.view().active), "look", err); // saturating, not `- 1`: `f.zig:0-5` is a legal thing to type and a // 1-based zero is the same nothing an absent number is - const acol: i32 = @intCast(at.col -| 1); - const ecol: i32 = if (at.end_col > 0) @intCast(at.end_col - 1) else std.math.maxInt(i32); - const r = panes.Text.cellRange(text, @intCast(at.line -| 1), acol, @intCast(at.end_line -| 1), ecol); + // Any number a place has: cellRange clamps each to the text. + const acol = std.math.lossyCast(i32, at.col -| 1); + const ecol: i32 = if (at.end_col > 0) std.math.lossyCast(i32, at.end_col - 1) else std.math.maxInt(i32); + const r = panes.Text.cellRange(text, std.math.lossyCast(i32, at.line -| 1), acol, std.math.lossyCast(i32, at.end_line -| 1), ecol); pane.body.setRange(p.settings.tab, text, 0, r, true); } @@ -1590,7 +1599,7 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void { const free = p.freeSlot() orelse return p.reportError(.at(id), "look", error.NoPaneSlots); const nt = panes.File.open(p, free, target.path, target.at.line) catch |err| return lookOpenFailed(p, id, target.path, err); - if (target.at.col > 0) nt.body.cur_col = @intCast(target.at.col - 1); + nt.body.cur_col = columnOn(nt, nt.body.cur_row, target.at.col); const failures = p.fs.failures; layout.placeNew(p, id, id, free, .doc); // Refused a place: that is the look's one failure, not a @@ -2010,6 +2019,25 @@ test "Look copies its operand before releasing pointer selection storage" { try std.testing.expectEqual(.none, p.view().drag); } +test "a Look whose column is past its line's end lands at the end, however big; a line past the file's is a miss" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12 }); + defer p.deinit(); + const pane = try p.setTestFile("alpha\n\tbeta\n"); + // Up to 2026-11-23 a column past i32 panicked the session (`@intCast` + // in focusPaneLine): any text a B3 or Look takes, a server's row. + for ([_][]const u8{ "/test.txt:2:9", "/test.txt:2:3000000000", "/test.txt:2:18446744073709551615" }) |place| { + lookAt(p, 0, place); + try std.testing.expectEqual(@as(i32, 1), pane.body.cur_row); + try std.testing.expectEqual(@as(i32, 5), pane.body.cur_col); + } + // A span past the text: selected to its end. + lookAt(p, 0, "/test.txt:1:2-3000000000:3000000000"); + try std.testing.expect(pane.body.vsel.active); + pane.body.cur_row = 0; + lookAt(p, 0, "/test.txt:3000000000:1"); + try std.testing.expectEqual(@as(i32, 0), pane.body.cur_row); +} + test "@p names a pane by its serial, as /index does" { const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12 }); defer p.deinit(); diff --git a/test/pdf-fixtures/huge-pages.pdf b/test/pdf-fixtures/huge-pages.pdf new file mode 100644 index 00000000..f2d73a14 --- /dev/null +++ b/test/pdf-fixtures/huge-pages.pdf @@ -0,0 +1,61 @@ +%PDF-1.4 +1 0 obj +<< /Type /Catalog /Pages 2 0 R >> +endobj +2 0 obj +<< /Type /Pages /Kids [3 0 R 5 0 R 7 0 R 9 0 R] /Count 4 >> +endobj +3 0 obj +<< /Type /Page /Parent 2 0 R /MediaBox [0 0 1000000000 792] /Contents 4 0 R >> +endobj +4 0 obj +<< /Length 17 >> +stream +0 0 m 100 100 l S +endstream +endobj +5 0 obj +<< /Type /Page /Parent 2 0 R /MediaBox [0 0 1000000000000000000000000000000000000000000000 1000000000000000000000000000000000000000000000] /Contents 6 0 R >> +endobj +6 0 obj +<< /Length 17 >> +stream +0 0 m 100 100 l S +endstream +endobj +7 0 obj +<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /UserUnit 1000000000000000000000000000000000000 /Contents 8 0 R >> +endobj +8 0 obj +<< /Length 17 >> +stream +0 0 m 100 100 l S +endstream +endobj +9 0 obj +<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 10 0 R >> +endobj +10 0 obj +<< /Length 17 >> +stream +0 0 m 100 100 l S +endstream +endobj +xref +0 11 +0000000000 65535 f +0000000009 00000 n +0000000058 00000 n +0000000133 00000 n +0000000227 00000 n +0000000294 00000 n +0000000467 00000 n +0000000534 00000 n +0000000669 00000 n +0000000736 00000 n +0000000824 00000 n +trailer +<< /Size 11 /Root 1 0 R >> +startxref +892 +%%EOF |
