fix(Buffer): prevent integer bit truncation crash in files with extremely long lines

This commit is contained in:
CJ van den Berg 2024-07-02 12:43:52 +02:00
parent 5164164fd7
commit 581dbfb749
2 changed files with 5 additions and 5 deletions

View file

@ -517,16 +517,16 @@ const Node = union(enum) {
return pred(ecg); return pred(ecg);
} }
pub fn get_line_width_map(self: *const Node, line: usize, map: *ArrayList(u16), plane: Plane) error{ Stop, NoSpaceLeft }!void { pub fn get_line_width_map(self: *const Node, line: usize, map: *ArrayList(usize), plane: Plane) error{ Stop, NoSpaceLeft }!void {
const Ctx = struct { const Ctx = struct {
map: *ArrayList(u16), map: *ArrayList(usize),
wcwidth: usize = 0, wcwidth: usize = 0,
fn walker(ctx_: *anyopaque, egc: []const u8, wcwidth: usize, _: Plane) Walker { fn walker(ctx_: *anyopaque, egc: []const u8, wcwidth: usize, _: Plane) Walker {
const ctx = @as(*@This(), @ptrCast(@alignCast(ctx_))); const ctx = @as(*@This(), @ptrCast(@alignCast(ctx_)));
var n = egc.len; var n = egc.len;
while (n > 0) : (n -= 1) { while (n > 0) : (n -= 1) {
const p = ctx.map.addOne() catch |e| return .{ .err = e }; const p = ctx.map.addOne() catch |e| return .{ .err = e };
p.* = @intCast(ctx.wcwidth); p.* = ctx.wcwidth;
} }
ctx.wcwidth += wcwidth; ctx.wcwidth += wcwidth;
return if (egc[0] == '\n') Walker.stop else Walker.keep_walking; return if (egc[0] == '\n') Walker.stop else Walker.keep_walking;

View file

@ -3856,7 +3856,7 @@ pub const EditorWidget = struct {
}; };
pub const PosToWidthCache = struct { pub const PosToWidthCache = struct {
cache: std.ArrayList(u16), cache: std.ArrayList(usize),
cached_line: usize = std.math.maxInt(usize), cached_line: usize = std.math.maxInt(usize),
cached_root: ?Buffer.Root = null, cached_root: ?Buffer.Root = null,
@ -3864,7 +3864,7 @@ pub const PosToWidthCache = struct {
pub fn init(a: Allocator) !Self { pub fn init(a: Allocator) !Self {
return .{ return .{
.cache = try std.ArrayList(u16).initCapacity(a, 2048), .cache = try std.ArrayList(usize).initCapacity(a, 2048),
}; };
} }