Ensure buffer change cb does not read bad cursor #16647
Closed
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.
In multiple editing operations the cursor may temporarily be out of bounds during calls into user code from buffer update listeners. This means doing anything in a change callback that depends on the cursor position - including calling any function from Vim script - can throw an error. Moving the cursor is also not possible, since there is no way to restore the cursor to its previous invalid position via the public APIs.
This pull request attempts to fix the issue by always saving the location of the cursor before running each change callback with the cursor in a valid spot.
For some background, I fixed one instance of this in #11782, but there are many more, as evidenced by these issue reports for the Neovim version of vim-strip-trailing-whitespace, all of which hit this issue:
Neovim: Error with undo without EOL axelf4/vim-strip-trailing-whitespace#5
(has reproduction steps)
E5108 error when nvim-spectre is installed axelf4/vim-strip-trailing-whitespace#8
Conflicts with floaterm in neovim axelf4/vim-strip-trailing-whitespace#9