Skip to content

fix(win32): prevent typeahead buffer overflow during mouse drag#19851

Open
mattn wants to merge 1 commit intovim:masterfrom
mattn:fix-typeahead-buffer-overflow
Open

fix(win32): prevent typeahead buffer overflow during mouse drag#19851
mattn wants to merge 1 commit intovim:masterfrom
mattn:fix-typeahead-buffer-overflow

Conversation

@mattn
Copy link
Copy Markdown
Member

@mattn mattn commented Mar 28, 2026

The typeahead buffer guard in mch_inchar() only reserved 5 bytes per loop iteration, but a mouse event writes up to 7 bytes (3 header + 4 coordinates) and a scroll event with modifiers writes up to 10 bytes. When dragging the status line quickly on Windows console, 3+ mouse events could queue up in a single loop pass and overflow the 20-byte static buffer, corrupting adjacent memory. This caused garbage bytes to be fed into the input stream, triggering unintended commands such as Ctrl-Z (suspend).
The bug has existed since Vim 7.0001 but was not visible on the legacy console where ReadConsole properly skipped MOUSE_EVENT records. With ConPTY (Windows Terminal), the corruption becomes visible.
Change the guard from 5 to 10 to match the actual worst-case write size.

The typeahead buffer guard in mch_inchar() only reserved 5 bytes per
iteration, but a mouse event writes up to 7 bytes (3 header + 4
coordinates) and a scroll event with modifiers writes up to 10 bytes
(3 modifier + 3 scroll + 4 coordinates).  During fast mouse dragging,
3+ events could queue up and overflow the 20-byte buffer, corrupting
adjacent static variables and causing garbage bytes (including Ctrl-Z)
to be fed into the input stream, which triggered nv_suspend/ex_stop.

Change the guard from 5 to 10 to account for the worst case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant