diff options
author | zeertzjq <zeertzjq@outlook.com> | 2022-05-27 05:12:59 +0800 |
---|---|---|
committer | zeertzjq <zeertzjq@outlook.com> | 2022-07-07 07:00:11 +0800 |
commit | df4c634d067cf01adca75abb5f38989777c5cddd (patch) | |
tree | 62f3beb3811b71d7712662a69ed82cbebfecab23 | |
parent | 4ffe5d018919d3fbea21e667f09f14b9751f9cd5 (diff) | |
download | rneovim-df4c634d067cf01adca75abb5f38989777c5cddd.tar.gz rneovim-df4c634d067cf01adca75abb5f38989777c5cddd.tar.bz2 rneovim-df4c634d067cf01adca75abb5f38989777c5cddd.zip |
vim-patch:8.2.5023: substitute overwrites allocated buffer
Problem: Substitute overwrites allocated buffer.
Solution: Disallow undo when in a substitute command.
https://github.com/vim/vim/commit/338f1fc0ee3ca929387448fe464579d6113fa76a
-rw-r--r-- | src/nvim/normal.c | 36 | ||||
-rw-r--r-- | src/nvim/testdir/test_substitute.vim | 22 | ||||
-rw-r--r-- | src/nvim/undo.c | 6 |
3 files changed, 45 insertions, 19 deletions
diff --git a/src/nvim/normal.c b/src/nvim/normal.c index 3a552ef053..9696130070 100644 --- a/src/nvim/normal.c +++ b/src/nvim/normal.c @@ -432,6 +432,18 @@ static int find_command(int cmdchar) return idx; } +/// If currently editing a cmdline or text is locked: beep and give an error +/// message, return true. +static bool check_text_locked(oparg_T *oap) +{ + if (text_locked()) { + clearopbeep(oap); + text_locked_msg(); + return true; + } + return false; +} + /// Normal state entry point. This is called on: /// /// - Startup, In this case the function never returns. @@ -1079,15 +1091,9 @@ static int normal_execute(VimState *state, int key) goto finish; } - if (text_locked() && (nv_cmds[s->idx].cmd_flags & NV_NCW)) { - // This command is not allowed while editing a cmdline: beep. - clearopbeep(&s->oa); - text_locked_msg(); - s->command_finished = true; - goto finish; - } - - if ((nv_cmds[s->idx].cmd_flags & NV_NCW) && curbuf_locked()) { + if ((nv_cmds[s->idx].cmd_flags & NV_NCW) + && (check_text_locked(&s->oa) || curbuf_locked())) { + // this command is not allowed now s->command_finished = true; goto finish; } @@ -4704,9 +4710,7 @@ static void nv_gotofile(cmdarg_T *cap) char_u *ptr; linenr_T lnum = -1; - if (text_locked()) { - clearopbeep(cap->oap); - text_locked_msg(); + if (check_text_locked(cap->oap)) { return; } if (curbuf_locked()) { @@ -6433,13 +6437,7 @@ static void nv_g_cmd(cmdarg_T *cap) // "gQ": improved Ex mode case 'Q': - if (text_locked()) { - clearopbeep(cap->oap); - text_locked_msg(); - break; - } - - if (!checkclearopq(oap)) { + if (!check_text_locked(cap->oap) && !checkclearopq(oap)) { do_exmode(); } break; diff --git a/src/nvim/testdir/test_substitute.vim b/src/nvim/testdir/test_substitute.vim index bd44079882..e06da3b44f 100644 --- a/src/nvim/testdir/test_substitute.vim +++ b/src/nvim/testdir/test_substitute.vim @@ -851,6 +851,28 @@ func Test_sub_change_window() delfunc Repl endfunc +" This was undoign a change in between computing the length and using it. +func Do_Test_sub_undo_change() + new + norm o0000000000000000000000000000000000000000000000000000 + silent! s/\%')/\=Repl() + bwipe! +endfunc + +func Test_sub_undo_change() + func Repl() + silent! norm g- + endfunc + call Do_Test_sub_undo_change() + + func! Repl() + silent earlier + endfunc + call Do_Test_sub_undo_change() + + delfunc Repl +endfunc + " Test for the 2-letter and 3-letter :substitute commands func Test_substitute_short_cmd() new diff --git a/src/nvim/undo.c b/src/nvim/undo.c index 1c0b7aa669..8324db37c6 100644 --- a/src/nvim/undo.c +++ b/src/nvim/undo.c @@ -89,6 +89,7 @@ #include "nvim/change.h" #include "nvim/cursor.h" #include "nvim/edit.h" +#include "nvim/ex_getln.h" #include "nvim/extmark.h" #include "nvim/fileio.h" #include "nvim/fold.h" @@ -1954,6 +1955,11 @@ void undo_time(long step, bool sec, bool file, bool absolute) bool above = false; bool did_undo = true; + if (text_locked()) { + text_locked_msg(); + return; + } + // First make sure the current undoable change is synced. if (curbuf->b_u_synced == false) { u_sync(true); |