Blame SOURCES/0001-patch-8.2.4977-memory-access-error-when-substitute-e.patch

f520a9
diff -up vim82/src/ex_cmds.c.cve1785 vim82/src/ex_cmds.c
f520a9
--- vim82/src/ex_cmds.c.cve1785	2022-06-10 10:26:16.883312704 +0200
f520a9
+++ vim82/src/ex_cmds.c	2022-06-10 10:26:16.910312568 +0200
f520a9
@@ -4356,12 +4356,17 @@ ex_substitute(exarg_T *eap)
f520a9
 		// Save flags for recursion.  They can change for e.g.
f520a9
 		// :s/^/\=execute("s#^##gn")
f520a9
 		subflags_save = subflags;
f520a9
+
f520a9
+		// Disallow changing text or switching window in an expression.
f520a9
+		++textwinlock;
f520a9
 #endif
f520a9
 		// get length of substitution part
f520a9
 		sublen = vim_regsub_multi(&regmatch,
f520a9
 				    sub_firstlnum - regmatch.startpos[0].lnum,
f520a9
 			       sub, sub_firstline, FALSE, magic_isset(), TRUE);
f520a9
 #ifdef FEAT_EVAL
f520a9
+		--textwinlock;
f520a9
+
f520a9
 		// If getting the substitute string caused an error, don't do
f520a9
 		// the replacement.
f520a9
 		// Don't keep flags set by a recursive call.
f520a9
@@ -4462,9 +4467,15 @@ ex_substitute(exarg_T *eap)
f520a9
 		mch_memmove(new_end, sub_firstline + copycol, (size_t)copy_len);
f520a9
 		new_end += copy_len;
f520a9
 
f520a9
+#ifdef FEAT_EVAL
f520a9
+		++textwinlock;
f520a9
+#endif
f520a9
 		(void)vim_regsub_multi(&regmatch,
f520a9
 				    sub_firstlnum - regmatch.startpos[0].lnum,
f520a9
 				      sub, new_end, TRUE, magic_isset(), TRUE);
f520a9
+#ifdef FEAT_EVAL
f520a9
+		--textwinlock;
f520a9
+#endif
f520a9
 		sub_nsubs++;
f520a9
 		did_sub = TRUE;
f520a9
 
f520a9
diff -up vim82/src/testdir/test_substitute.vim.cve1785 vim82/src/testdir/test_substitute.vim
f520a9
--- vim82/src/testdir/test_substitute.vim.cve1785	2022-06-10 10:26:16.910312568 +0200
f520a9
+++ vim82/src/testdir/test_substitute.vim	2022-06-10 10:27:02.166084629 +0200
f520a9
@@ -942,5 +942,18 @@ func Test_using_old_sub()
f520a9
   set nocompatible
f520a9
 endfunc
f520a9
 
f520a9
+" This was switching windows in between computing the length and using it.
f520a9
+func Test_sub_change_window()
f520a9
+  silent! lfile
f520a9
+  sil! norm o0000000000000000000000000000000000000000000000000000
f520a9
+  func Repl()
f520a9
+    lopen
f520a9
+  endfunc
f520a9
+  silent!  s/\%')/\=Repl()
f520a9
+  bwipe!
f520a9
+  bwipe!
f520a9
+  delfunc Repl
f520a9
+endfunc
f520a9
+
f520a9
 
f520a9
 " vim: shiftwidth=2 sts=2 expandtab