Skip to content

selection_extract: clamp row index before grid access (fixes segfault on drag-to-other-window)#3

Open
blshkv wants to merge 1 commit into
isene:masterfrom
blshkv:fix/selection-extract-oob-segfault
Open

selection_extract: clamp row index before grid access (fixes segfault on drag-to-other-window)#3
blshkv wants to merge 1 commit into
isene:masterfrom
blshkv:fix/selection-extract-oob-segfault

Conversation

@blshkv

@blshkv blshkv commented Jul 21, 2026

Copy link
Copy Markdown

Closes #2

sel_start_row and sel_end_row are clamped to MAX_ROWS-1 during normal operation, but selection_extract computes a shifted row index that can exceed MAX_ROWS-1 when the selection extends into the scrollback buffer and scroll_offset is non-zero. The resulting grid pointer lands past the end of the buffer, causing a segfault.

Fix: add bounds checks before both grid-pointer computations in selection_extract.

🤖 Generated with Claude Code

sel_start_row/sel_end_row can legitimately exceed MAX_ROWS-1 during
scrollback selection (incremented in lockstep with scroll_offset so
the .se_row_grid_shifted path keeps them in bounds). If scroll_offset
is reset to 0 before a SelectionRequest arrives (e.g. the user drags
selected text to another window after the viewport snapped back), the
.se_row_live path fires instead and computes

    grid + r12 * MAX_COLS * CELL_SIZE

with r12 >= MAX_ROWS, producing an out-of-bounds read and segfault.

Add a MAX_ROWS guard in both .se_row_live and .se_row_grid_shifted
before the imul/lea sequence. An out-of-range row jumps to .se_done,
truncating the selection at the valid region rather than crashing.

Fixes: isene#2

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@blshkv
blshkv force-pushed the fix/selection-extract-oob-segfault branch from 10cd293 to c28ec4c Compare July 21, 2026 14:15
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.

Segfault in selection_extract when dragging selected text to another window

1 participant