summaryrefslogtreecommitdiff
path: root/core/Query.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/Query.zig')
-rw-r--r--core/Query.zig35
1 files changed, 24 insertions, 11 deletions
diff --git a/core/Query.zig b/core/Query.zig
index f91339ca..228cdd0e 100644
--- a/core/Query.zig
+++ b/core/Query.zig
@@ -1259,19 +1259,21 @@ pub fn take(p: *Pardes, q: *Query, kind: Stream.Kind, rows: []const u8) void {
const t = look.parsePathLine(ln);
if (t.at.line == 0 or t.path.len == 0) continue;
const code = if (t.end < ln.len) ln[t.end + 1 ..] else "";
- const path_at: u32 = @intCast(q.found_bytes.items.len);
+ // A row is a line's text, a file's name in it unescaped (one
+ // named `a:5000000000:b` reads as `a` at line 5000000000): one
+ // whose numbers a Found cannot hold is passed over.
+ const found: Found = .{
+ .path = std.math.cast(u32, q.found_bytes.items.len) orelse return,
+ .path_len = std.math.cast(u16, t.path.len) orelse continue,
+ .line = std.math.cast(u32, t.at.line) orelse continue,
+ .col = std.math.cast(u32, t.at.col -| 1) orelse continue,
+ .end = std.math.cast(u32, if (t.at.end_col > 0) t.at.end_col else t.at.col) orelse continue,
+ .code = std.math.cast(u32, q.found_bytes.items.len + t.path.len) orelse return,
+ .code_len = std.math.cast(u32, code.len) orelse continue,
+ };
q.found_bytes.appendSlice(p.gpa, t.path) catch return;
- const code_at: u32 = @intCast(q.found_bytes.items.len);
q.found_bytes.appendSlice(p.gpa, code) catch return;
- q.found.append(p.gpa, .{
- .path = path_at,
- .path_len = @intCast(t.path.len),
- .line = @intCast(t.at.line),
- .col = @intCast(t.at.col -| 1),
- .end = @intCast(if (t.at.end_col > 0) t.at.end_col else t.at.col),
- .code = code_at,
- .code_len = @intCast(code.len),
- }) catch return;
+ q.found.append(p.gpa, found) catch return;
},
}
}
@@ -2749,3 +2751,14 @@ test "a query row's address reaches every way a look arrives: an empty look writ
try std.testing.expectEqual(tree.Status.ok, @import("ninep/harness.zig").wr(p, tree.Node.of(qp.serial, .look), line).reply.status);
try std.testing.expectEqual(@as(i32, 3), a.body.cur_row);
}
+
+test "Grep: a row whose numbers a Found cannot hold (a column past u32) is passed over, never cast" {
+ const gpa = std.testing.allocator;
+ const p = try @import("ninep/harness.zig").withFile(gpa, "x\n");
+ defer p.deinit();
+ var q: Query = .{ .owner = 0 };
+ defer q.deinit(p.gpa);
+ take(p, &q, .grep, "a:5000000000:b:3:1-4 hit\nok.txt:2:3-5 hit\nb:7:4294967297 hit\n");
+ // Taken or passed over, each: never cast (main: Query.zig:1267).
+ try std.testing.expect(q.found.items.len <= 3);
+}