aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbfredl <bjorn.linse@gmail.com>2023-08-27 11:26:54 +0200
committerbfredl <bjorn.linse@gmail.com>2023-08-27 12:08:11 +0200
commit840749d6c971f93aa9744bd6f76b383f11043463 (patch)
treee9ed01feacbc4be34b96faf46e4baa1a8c83de16
parent9b9030ff2ca820d4c4f4b559f86b0f9a3496645b (diff)
downloadrneovim-840749d6c971f93aa9744bd6f76b383f11043463.tar.gz
rneovim-840749d6c971f93aa9744bd6f76b383f11043463.tar.bz2
rneovim-840749d6c971f93aa9744bd6f76b383f11043463.zip
fix(undo): fix crash caused by checking undolevels in wrong buffer
fixes #24894
-rw-r--r--src/nvim/undo.c6
-rw-r--r--test/functional/api/buffer_spec.lua9
2 files changed, 11 insertions, 4 deletions
diff --git a/src/nvim/undo.c b/src/nvim/undo.c
index 552120d4ae..1194eeca35 100644
--- a/src/nvim/undo.c
+++ b/src/nvim/undo.c
@@ -3208,15 +3208,13 @@ u_header_T *u_force_get_undo_header(buf_T *buf)
}
// Create the first undo header for the buffer
if (!uhp) {
- // Undo is normally invoked in change code, which already has swapped
- // curbuf.
// Args are tricky: this means replace empty range by empty range..
- u_savecommon(curbuf, 0, 1, 1, true);
+ u_savecommon(buf, 0, 1, 1, true);
uhp = buf->b_u_curhead;
if (!uhp) {
uhp = buf->b_u_newhead;
- if (get_undolevel(curbuf) > 0 && !uhp) {
+ if (get_undolevel(buf) > 0 && !uhp) {
abort();
}
}
diff --git a/test/functional/api/buffer_spec.lua b/test/functional/api/buffer_spec.lua
index fda2ea17b8..0afe619b03 100644
--- a/test/functional/api/buffer_spec.lua
+++ b/test/functional/api/buffer_spec.lua
@@ -16,6 +16,7 @@ local command = helpers.command
local bufmeths = helpers.bufmeths
local feed = helpers.feed
local pcall_err = helpers.pcall_err
+local assert_alive = helpers.assert_alive
describe('api/buf', function()
before_each(clear)
@@ -41,6 +42,14 @@ describe('api/buf', function()
eq(1, curbuf_depr('line_count'))
end)
+ it("doesn't crash just after set undolevels=1 #24894", function()
+ local buf = meths.create_buf(false, true)
+ meths.buf_set_option(buf, 'undolevels', -1)
+ meths.buf_set_lines(buf, 0, 1, false, { })
+
+ assert_alive()
+ end)
+
it('cursor position is maintained after lines are inserted #9961', function()
-- replace the buffer contents with these three lines.
request('nvim_buf_set_lines', 0, 0, -1, 1, {"line1", "line2", "line3", "line4"})