Skip to content

Commit 2530d19

Browse files
authored
fix(core): clip glyphs left of the buffer (#1361)
one sentence: check should not be inclusive. drawTextBufferInternal did not clip a width-1 glyph at currentX == -1. The transparent text path then calculated an invalid u32 index and read past the cell buffer. Clip each glyph when its range ends at or before column 0. Assert that the index is in the cell buffer before direct access. This fixes the abort in OpenCode issue [#42094](anomalyco/opencode#42094). The stack includes trySetTransparentTextCellFast and bufferDrawTextBufferView.
1 parent 14b3d13 commit 2530d19

3 files changed

Lines changed: 44 additions & 1 deletion

File tree

‎bun.lock‎

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/core/src/zig/buffer.zig‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,9 @@ pub const OptimizedBuffer = struct {
10181018
if (ansi.TextAttributes.getLinkId(attributes) != 0) return false;
10191019
if (gp.isGraphemeChar(char) or gp.isContinuationChar(char)) return false;
10201020

1021+
// The caller must clip the glyph before it calculates the index.
1022+
assert(index < self.buffer.char.len);
1023+
10211024
const dest_char = self.buffer.char[index];
10221025
const dest_attributes = self.buffer.attributes[index];
10231026
if (ansi.TextAttributes.getLinkId(dest_attributes) != 0) return false;
@@ -1768,7 +1771,12 @@ pub const OptimizedBuffer = struct {
17681771
break;
17691772
}
17701773

1771-
if (currentX < -@as(i32, @intCast(g_width))) {
1774+
// A glyph occupies columns [currentX, currentX + g_width).
1775+
// If this range ends at or before column 0, the glyph is left of the screen.
1776+
// Skip the glyph, but advance the counters to put the next glyph in the correct columns.
1777+
// This check permits wide glyphs that cross column 0.
1778+
// The g_width > 1 check below discards these glyphs before they reach the unchecked fast-path index.
1779+
if (currentX + @as(i32, @intCast(g_width)) <= 0) {
17721780
globalCharPos += g_width;
17731781
currentX += @as(i32, @intCast(g_width));
17741782
column_in_line += g_width;

‎packages/core/src/zig/tests/buffer_test.zig‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,25 @@ test "OptimizedBuffer text buffer tab clips a negative draw origin" {
541541
try std.testing.expectEqual(@as(u32, ' '), target.get(0, 0).?.char);
542542
}
543543

544+
test "OptimizedBuffer text buffer clips width-1 text at a negative draw origin" {
545+
const pool = gp.initGlobalPool(std.testing.allocator);
546+
defer gp.deinitGlobalPool();
547+
var local_link_pool = link.LinkPool.init(std.testing.allocator);
548+
defer local_link_pool.deinit();
549+
var text = try TextBuffer.init(std.testing.allocator, pool, &local_link_pool, .unicode);
550+
defer text.deinit();
551+
try text.setText("AB");
552+
var view = try TextBufferView.init(std.testing.allocator, text);
553+
defer view.deinit();
554+
555+
const target = try OptimizedBuffer.init(std.testing.allocator, 1, 1, .{ .pool = pool, .id = "negative-width1-text" });
556+
defer target.deinit();
557+
558+
target.drawTextBuffer(view, -1, 0);
559+
560+
try std.testing.expectEqual(@as(u32, 'B'), target.get(0, 0).?.char);
561+
}
562+
544563
test "OptimizedBuffer image-free frame buffer copy does not allocate" {
545564
var pool = gp.GraphemePool.init(std.testing.allocator);
546565
defer pool.deinit();

0 commit comments

Comments
 (0)