[PATCH] vi: fix undo when ':s' replacement string is empty

Ron Yorston rmy at pobox.com
Fri Jul 2 07:17:25 UTC 2021


When a ':s' search and replace command had an empty replacement
string a subsequent undo put the original text in the wrong place.

Fix this by skipping the insertion of an empty replacement string.

function                                             old     new   delta
colon                                               4024    4036     +12

Signed-off-by: Ron Yorston <rmy at pobox.com>
---
 editors/vi.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/editors/vi.c b/editors/vi.c
index f0bbc9518..7cbeb7113 100644
--- a/editors/vi.c
+++ b/editors/vi.c
@@ -3139,12 +3139,14 @@ static void colon(char *buf)
 					++lines;
 				}
 #  endif
-				// insert the "replace" patern
-				bias = string_insert(found, R, ALLOW_UNDO_CHAIN);
-				found += bias;
-				ls += bias;
+				// insert the "replace" pattern, if required
+				if (len_R) {
+					bias = string_insert(found, R, ALLOW_UNDO_CHAIN);
+					found += bias;
+					ls += bias;
+					//q += bias; - recalculated anyway
+				}
 				dot = ls;
-				//q += bias; - recalculated anyway
 				// check for "global"  :s/foo/bar/g
 				if (gflag == 'g') {
 					if ((found + len_R) < end_line(ls)) {
-- 
2.31.1



More information about the busybox mailing list