From 581dbfb7498c84ceebd2a76cf31093dab372e011 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Tue, 2 Jul 2024 12:43:52 +0200 Subject: [PATCH] fix(Buffer): prevent integer bit truncation crash in files with extremely long lines --- src/buffer/Buffer.zig | 6 +++--- src/tui/editor.zig | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/buffer/Buffer.zig b/src/buffer/Buffer.zig index bea4aff..06331ef 100644 --- a/src/buffer/Buffer.zig +++ b/src/buffer/Buffer.zig @@ -517,16 +517,16 @@ const Node = union(enum) { 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 { - map: *ArrayList(u16), + map: *ArrayList(usize), wcwidth: usize = 0, fn walker(ctx_: *anyopaque, egc: []const u8, wcwidth: usize, _: Plane) Walker { const ctx = @as(*@This(), @ptrCast(@alignCast(ctx_))); var n = egc.len; while (n > 0) : (n -= 1) { const p = ctx.map.addOne() catch |e| return .{ .err = e }; - p.* = @intCast(ctx.wcwidth); + p.* = ctx.wcwidth; } ctx.wcwidth += wcwidth; return if (egc[0] == '\n') Walker.stop else Walker.keep_walking; diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 021708d..5a8db21 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -3856,7 +3856,7 @@ pub const EditorWidget = struct { }; pub const PosToWidthCache = struct { - cache: std.ArrayList(u16), + cache: std.ArrayList(usize), cached_line: usize = std.math.maxInt(usize), cached_root: ?Buffer.Root = null, @@ -3864,7 +3864,7 @@ pub const PosToWidthCache = struct { pub fn init(a: Allocator) !Self { return .{ - .cache = try std.ArrayList(u16).initCapacity(a, 2048), + .cache = try std.ArrayList(usize).initCapacity(a, 2048), }; }