fix: validate line exists before setting extmark in mark_above_range#136
Closed
funkybooboo wants to merge 1 commit intoThePrimeagen:masterfrom
Closed
fix: validate line exists before setting extmark in mark_above_range#136funkybooboo wants to merge 1 commit intoThePrimeagen:masterfrom
funkybooboo wants to merge 1 commit intoThePrimeagen:masterfrom
Conversation
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


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:
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:
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
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_rangenow checks that the target line exists before computing its end column and setting an extmark, andMark:is_valid,Mark:set_virtual_text,Mark:delete, andPoint.from_marknow explicitly handle missingidvalues.Written by Cursor Bugbot for commit 899db4e. This will update automatically on new commits. Configure here.