fix(buffer): guard set_stringn against out-of-area coordinates (#2810) by ronitgupta138 · Pull Request #2850 · ratatui/ratatui · GitHub
Skip to content

fix(buffer): guard set_stringn against out-of-area coordinates (#2810) - #2850

Merged
joshka merged 2 commits into
ratatui:mainfrom
ronitgupta138:fix/buffer-index-degenerate-rect
Oct 21, 2026
Merged

joshka merged 2 commits into
ratatui:mainfrom
ronitgupta138:fix/buffer-index-degenerate-rect

Conversation

@ronitgupta138

Copy link
Copy Markdown
Contributor

Problem

In #2810, Buffer::set_stringn panicked when called with coordinates outside the buffer's area (e.g., in a 57x1 terminal split inside tmux where a multi-row layout writes to row y = 1).

Buffer::set_stringn computed remaining_width from self.area.right().saturating_sub(x), but did not verify whether y fell within [area.top(), area.bottom()) or whether x was already outside the area. Writing to self[(x, y)] subsequently panicked in Buffer::index_of.

Solution

Add an early-return guard at the start of Buffer::set_stringn:

if y < self.area.top() || y >= self.area.bottom() || x >= self.area.right() {
    return (x, y);
}

If coordinates fall outside the buffer area, it safely early returns without mutating buffer contents or panicking, mirroring the safe behavior in Buffer::set_style.

Added unit test set_stringn_does_not_panic_when_out_of_area in ratatui-core/src/buffer/buffer.rs.

Fixes #2810.

@ronitgupta138
ronitgupta138 requested a review from a team as a code owner October 20, 2026 20:03
Copilot AI balanced review requested due to automatic review settings October 20, 2026 20:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

ronitgupta138 added a commit to ronitgupta138/ronitgupta138 that referenced this pull request Oct 20, 2026
@codecov

codecov Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

…ui#2810)

Buffer::set_stringn previously only calculated remaining width based on
x and self.area.right(), without checking if y or x fell outside the
buffer's bounded Rect. When multi-row layouts are rendered into degenerate
or collapsed terminal viewports (e.g. 57x1 in tmux or narrow splits),
writing to row y >= 1 indexed out of bounds into the buffer, triggering
a panic in Buffer::index_of.

Add boundary checks in Buffer::set_stringn to early return (x, y) if y is
outside [area.top(), area.bottom()) or x >= area.right(), preventing panics
during degenerate renders without mutating buffer state.

Signed-off-by: Ronit Gupta <ronitgupta138@gmail.com>
@ronitgupta138
ronitgupta138 force-pushed the fix/buffer-index-degenerate-rect branch from 8813bc3 to 8d85101 Compare October 20, 2026 20:07
@joshka

joshka commented Oct 21, 2026

Copy link
Copy Markdown
Member

@joshka
joshka merged commit f57b06f into ratatui:main Oct 21, 2026
41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Buffer::index panics with index outside of buffer on degenerate Rect areas

3 participants