diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-23 09:15:57 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-23 09:25:44 -0300 |
| commit | c8f0d65a69274dc8a072c35a6c13cb07a6770007 (patch) | |
| tree | b3e13754d99250ee596be3368d07539eb4437bfb /addons | |
| parent | 0733d0a0d89bacb64f45d71b06ffbcf1abe6ffb3 (diff) | |
| download | pardes-c8f0d65a69274dc8a072c35a6c13cb07a6770007.tar.gz pardes-c8f0d65a69274dc8a072c35a6c13cb07a6770007.zip | |
pdf, abi, core: the pointer and count a C struct or a C caller hands over read through one helper, `addon.cSlice` (abi/addon.zig), NULL being none whatever the count: the bridge says none with a NULL (pardes_pdf_search_page with no hit, pardes_pdf_select over no text, pardes_pdf_load_outline of no entries: each OK with a NULL and a count of 0), and Zig slices a NULL C pointer with a safety panic even at length 0; every site in pdf.zig (jobRun's page text and hits, pageText, hitsOf, selectHere, probeAt, follow, the outline's usable, resolve, sections and section, its bytes now bounded by bytes_len), abi/addon.zig's own (its private `span` was that helper for bytes alone; open's bytes), and the core's kind-ABI callbacks (Kind.zig look, say, text, exec, select, found; Addon.zig loadedOf's words), the core taking the addons' module (`addon`) for it; what holds the next site: pdf.zig's test (run by addon-test, which now runs the pdf module's tests) takes every typed pointer field of the bridge's structs from the translated header at comptime and fails on any `.field[` in pdf.zig, naming the line (shown by putting one site back: "pdf.zig:1014: pardes_pdf_text.data read bare"); the test of the change before passes, with every PDF behaviour test (8/8, Debug)
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'addons')
| -rw-r--r-- | addons/pdf/pdf.zig | 57 |
1 files changed, 38 insertions, 19 deletions
diff --git a/addons/pdf/pdf.zig b/addons/pdf/pdf.zig index 8f314bbe..79d79219 100644 --- a/addons/pdf/pdf.zig +++ b/addons/pdf/pdf.zig @@ -759,7 +759,7 @@ pub const Pdf = extern struct { const found = in.n > 0 and m.pardes_pdf_search_page(ld, @intCast(in.page), &q, &r) == ok; defer if (found) m.pardes_pdf_drop_search_result(ld, r.handle); res.ok, res.n, res.hits = .{ 1, @intCast(t.len), if (found) @intCast(r.hit_count) else 0 }; - return answer(gpa, res, std.mem.concat(gpa, u8, &.{ t.data[0..t.len], if (found) std.mem.sliceAsBytes(r.quads[0..r.quad_count]) else "" }) catch return null); + return answer(gpa, res, std.mem.concat(gpa, u8, &.{ addon.cSlice(t.data, t.len), if (found) std.mem.sliceAsBytes(addon.cSlice(r.quads, r.quad_count)) else "" }) catch return null); } // each page's count of hits, then the rows var shown = @min(in.n, 200); @@ -1011,7 +1011,7 @@ fn pageText(o: *Own, page: u32) []const u8 { var t: m.pardes_pdf_text = undefined; if (m.pardes_pdf_page_text(d.base, @intCast(page), &t) != ok) return ""; defer m.pardes_pdf_drop_text(d.base, t.handle); - o.read.bytes = heap.dupe(u8, t.data[0..t.len]) catch &.{}; + o.read.bytes = heap.dupe(u8, addon.cSlice(t.data, t.len)) catch &.{}; return o.read.bytes; } @@ -1030,7 +1030,7 @@ fn hitsOf(st: *const Pdf, o: *Own) []const m.pardes_pdf_search_quad { var r: m.pardes_pdf_search_result = undefined; if (m.pardes_pdf_search_page(d.base, @intCast(f.hits_page), &q, &r) != ok) return &.{}; defer m.pardes_pdf_drop_search_result(d.base, r.handle); - o.hits = heap.dupe(m.pardes_pdf_search_quad, r.quads[0..r.quad_count]) catch &.{}; + o.hits = heap.dupe(m.pardes_pdf_search_quad, addon.cSlice(r.quads, r.quad_count)) catch &.{}; return o.hits; } @@ -1073,8 +1073,8 @@ fn selectHere(o: *Own, page: u32, from: Point, to: Point) ?usize { var t: m.pardes_pdf_owned_text = undefined; if (m.pardes_pdf_copy_selection(base, @intCast(page), sel.start, sel.end, &t) != ok) return null; defer m.pardes_pdf_drop_owned_text(base, t.handle); - o.sel_quads = heap.dupe(Quad, sel.quads[0..sel.quad_count]) catch &.{}; - o.sel_text = heap.dupe(u8, t.data[0..t.len]) catch &.{}; + o.sel_quads = heap.dupe(Quad, addon.cSlice(sel.quads, sel.quad_count)) catch &.{}; + o.sel_text = heap.dupe(u8, addon.cSlice(t.data, t.len)) catch &.{}; return sel.quad_count; } @@ -1691,11 +1691,11 @@ fn probeAt(st: *Pdf, id: u8, col: u16, row: u16) ?struct { page: u32, quad: Quad const uri = struct { var buf: [1024]u8 = undefined; }; - @memcpy(uri.buf[0..link.uri_len], link.uri[0..link.uri_len]); + @memcpy(uri.buf[0..link.uri_len], addon.cSlice(link.uri, link.uri_len)); link.uri = &uri.buf; var text: []u8 = &.{}; if (m.pardes_pdf_copy_rectangle(base, @intCast(at.page), link.quad, &t) == ok) { - text = heap.dupe(u8, t.data[0..t.len]) catch &.{}; + text = heap.dupe(u8, addon.cSlice(t.data, t.len)) catch &.{}; m.pardes_pdf_drop_owned_text(base, t.handle); } return .{ .page = at.page, .quad = link.quad, .text = text, .link = link }; @@ -1706,7 +1706,7 @@ fn probeAt(st: *Pdf, id: u8, col: u16, row: u16) ?struct { page: u32, quad: Quad if (sel.quad_count == 0 or m.pardes_pdf_copy_selection(base, @intCast(at.page), sel.start, sel.end, &t) != ok) return null; defer m.pardes_pdf_drop_owned_text(base, t.handle); if (t.len == 0) return null; - return .{ .page = at.page, .quad = sel.quads[0], .text = heap.dupe(u8, t.data[0..t.len]) catch return null, .link = null }; + return .{ .page = at.page, .quad = addon.cSlice(sel.quads, sel.quad_count)[0], .text = heap.dupe(u8, addon.cSlice(t.data, t.len)) catch return null, .link = null }; } /// A link followed: a place in the document scrolled to, else looked at @@ -1714,7 +1714,7 @@ fn probeAt(st: *Pdf, id: u8, col: u16, row: u16) ?struct { page: u32, quad: Quad /// both, a +Links. fn follow(st: *Pdf, link: m.pardes_pdf_link, text: []const u8) void { if (link.destination_kind == m.PARDES_PDF_OUTLINE_DESTINATION_INTERNAL) return goToXY(st, @intCast(link.page), link.has_x != 0, link.x, link.has_y != 0, link.y); - const uri = link.uri[0..link.uri_len]; + const uri = addon.cSlice(link.uri, link.uri_len); var shown: [512]u8 = undefined; var k: usize = 0; for (text) |ch| if (ch > ' ' and k < shown.len) { @@ -1749,10 +1749,10 @@ fn goToXY(st: *Pdf, page: u32, has_x: bool, x: f32, has_y: bool, y: f32) void { /// from 1), indented by depth; an entry with nowhere to go takes its first /// child's that has, and an http(s) one is its URI. fn usable(o: *const m.pardes_pdf_outline_result, i: usize) bool { - const it = o.items[i]; + const it = addon.cSlice(o.items, o.item_count)[i]; if (it.destination_kind == m.PARDES_PDF_OUTLINE_DESTINATION_INTERNAL) return true; if (it.destination_kind != m.PARDES_PDF_OUTLINE_DESTINATION_EXTERNAL or it.uri_len == 0 or it.uri_len > 256) return false; - const u = o.bytes[it.uri_offset..][0..it.uri_len]; + const u = addon.cSlice(o.bytes, o.bytes_len)[it.uri_offset..][0..it.uri_len]; return (std.mem.startsWith(u8, u, "http://") or std.mem.startsWith(u8, u, "https://")) and for (u) |ch| { if (ch <= 0x20 or ch == 0x7f) break false; } else true; @@ -1760,9 +1760,10 @@ fn usable(o: *const m.pardes_pdf_outline_result, i: usize) bool { /// Entry `i`'s place: its own, else the first usable one below it. fn resolve(o: *const m.pardes_pdf_outline_result, i: usize) ?usize { - if (o.items[i].destination_kind != m.PARDES_PDF_OUTLINE_DESTINATION_NONE) return if (usable(o, i)) i else null; + const items = addon.cSlice(o.items, o.item_count); + if (items[i].destination_kind != m.PARDES_PDF_OUTLINE_DESTINATION_NONE) return if (usable(o, i)) i else null; var k = i + 1; - while (k < o.item_count and o.items[k].depth > o.items[i].depth) : (k += 1) if (usable(o, k)) return k; + while (k < items.len and items[k].depth > items[i].depth) : (k += 1) if (usable(o, k)) return k; return null; } @@ -1774,13 +1775,15 @@ fn sections(st: *Pdf) void { const rows = heap.alloc(u8, o.item_count * 300 + 1) catch return; defer heap.free(rows); var w: std.Io.Writer = .fixed(rows); - for (o.items[0..o.item_count], 0..) |it, i| { - const to = o.items[resolve(&o, i) orelse continue]; - if (to.destination_kind == m.PARDES_PDF_OUTLINE_DESTINATION_INTERNAL) w.print("{s}:{d}:{d} ", .{ ownOf(st).name, to.page + 1, i + 1 }) catch {} else w.print("{s} ", .{o.bytes[to.uri_offset..][0..to.uri_len]}) catch {}; + const items = addon.cSlice(o.items, o.item_count); + const bytes = addon.cSlice(o.bytes, o.bytes_len); + for (items, 0..) |it, i| { + const to = items[resolve(&o, i) orelse continue]; + if (to.destination_kind == m.PARDES_PDF_OUTLINE_DESTINATION_INTERNAL) w.print("{s}:{d}:{d} ", .{ ownOf(st).name, to.page + 1, i + 1 }) catch {} else w.print("{s} ", .{bytes[to.uri_offset..][0..to.uri_len]}) catch {}; w.splatByteAll(' ', 2 * it.depth) catch {}; // the title on one line: control and invalid characters a space // between words, none at either end - const t = o.bytes[it.title_offset..][0..it.title_len]; + const t = bytes[it.title_offset..][0..it.title_len]; const start = w.end; var gap = false; var i_: usize = 0; @@ -1811,9 +1814,9 @@ fn section(st: *Pdf, page: u32, ord: u32) bool { defer if (o.handle != null) m.pardes_pdf_drop_outline_result(base, o.handle); var said: [200]u8 = undefined; const r = if (m.pardes_pdf_load_outline(base, &o) == ok and ord > 0 and ord <= o.item_count) resolve(&o, ord - 1) else null; - const to = o.items[r orelse return miss(std.fmt.bufPrint(&said, "PdfSections: {s} has no section {d}", .{ ownOf(st).name, ord }))]; + const to = addon.cSlice(o.items, o.item_count)[r orelse return miss(std.fmt.bufPrint(&said, "PdfSections: {s} has no section {d}", .{ ownOf(st).name, ord }))]; if (to.destination_kind == m.PARDES_PDF_OUTLINE_DESTINATION_EXTERNAL) { - addon.look(o.bytes[to.uri_offset..][0..@min(to.uri_len, 256)]); + addon.look(addon.cSlice(o.bytes, o.bytes_len)[to.uri_offset..][0..@min(to.uri_len, 256)]); return true; } if (to.page + 1 != page) return miss(std.fmt.bufPrint(&said, "PdfSections: section {d} of {s} is on page {d}, not {d}", .{ ord, ownOf(st).name, to.page + 1, page })); @@ -1831,3 +1834,19 @@ fn miss(said: anyerror![]const u8) bool { fn noText() bool { return miss("has no text to address; its address is a page, :N"); } + +test "every pointer the bridge's structs hand back is read through addon.cSlice: none of them sliced or indexed bare (a NULL for none sliced at 0 was a release build's abort)" { + var src: []const u8 = @embedFile("pdf.zig"); // (searched at run time: at comptime it is over the branch quota) + _ = &src; + @setEvalBranchQuota(1_000_000); // (the translated header's every declaration) + inline for (@typeInfo(m).@"struct".decl_names) |name| { + if (comptime !std.mem.startsWith(u8, name, "pardes_pdf_") or @TypeOf(@field(m, name)) != type or @typeInfo(@field(m, name)) != .@"struct") continue; + const S = @typeInfo(@field(m, name)).@"struct"; + inline for (S.field_names, S.field_types) |field, F| if (comptime @typeInfo(F) == .pointer and @typeInfo(F).pointer.size == .c) { + if (std.mem.indexOf(u8, src, "." ++ field ++ "[")) |at| { + std.debug.print("pdf.zig:{d}: {s}.{s} read bare: addon.cSlice(ptr, count)\n", .{ std.mem.count(u8, src[0..at], "\n") + 1, name, field }); + return error.BareCPointer; + } + }; + } +} |
