Skip to content

fix: validate line exists before setting extmark in mark_above_range#136

Closed
funkybooboo wants to merge 1 commit intoThePrimeagen:masterfrom
funkybooboo:master
Closed

fix: validate line exists before setting extmark in mark_above_range#136
funkybooboo wants to merge 1 commit intoThePrimeagen:masterfrom
funkybooboo:master

Conversation

@funkybooboo
Copy link

@funkybooboo funkybooboo commented Feb 24, 2026

Fixes a crash when mark_above_range attempts to set an extmark on a line that no longer exists due to buffer modifications.

Problem

The original code assumed nvim_buf_get_lines() would always return a valid line:

local text = vim.api.nvim_buf_get_lines(buffer, above, above + 1, false)[1]
local ending = #text

When the buffer is modified (e.g., lines deleted during async operations), nvim_buf_get_lines() returns an empty table, causing text to be nil. Attempting to get the length of nil crashes with:

Error: Invalid 'line': out of range

This crash prevents the proper error handling in over-range.lua (lines 69-76) from running.

Solution

Added validation to check if the line exists before creating the extmark. If the line doesn't exist, the mark is not created (id remains nil). Updated is_valid() to return false when id is nil, allowing the existing validation logic in over-range.lua to properly detect the invalid mark and display the appropriate error message.

This approach ensures that when a buffer is modified and the target line no longer exists, the system correctly identifies the mark as invalid rather than creating a mark at an incorrect position.

Testing

  • All existing tests pass (11/11 marks tests)
  • Linting passes (0 warnings/errors)
  • Formatting passes (stylua check on marks.lua)
  • Manually verified fix resolves the crash during visual selection operations
  • Verified that invalid marks are properly detected by is_valid() check

Note

Low Risk
Small defensive changes around Neovim extmark creation/lookup; behavior change is limited to treating missing marks as invalid instead of erroring.

Overview
Prevents crashes when creating or operating on extmarks after buffer edits by allowing marks to be invalid (id == nil) and short-circuiting extmark API calls.

Mark.mark_above_range now checks that the target line exists before computing its end column and setting an extmark, and Mark:is_valid, Mark:set_virtual_text, Mark:delete, and Point.from_mark now explicitly handle missing id values.

Written by Cursor Bugbot for commit 899db4e. This will update automatically on new commits. Configure here.

Prevents 'Invalid line: out of range' error when buffer is modified
and the target line no longer exists. Falls back to line 0 as a safe
default when the line is not found.
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

end

function Mark:delete()
if not self.id then
Copy link

Choose a reason for hiding this comment

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

Missing nil id check in set_text_at_mark

Medium Severity

The set_text_at_mark method doesn't check if self.id is nil before calling Point.from_mark(self). When the mark has a nil id (as can now happen with mark_above_range when a line doesn't exist), Point.from_mark returns Point:from_1_based(0, 0), which converts to invalid coordinates (-1, -1) when passed to vim.api.nvim_buf_set_text, causing the operation to fail. This is inconsistent with set_virtual_text and delete, which both guard against nil ids.

Fix in Cursor Fix in Web

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.

1 participant