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

3e779a
diff -up vim80/src/ex_cmds.c.cve1785 vim80/src/ex_cmds.c
3e779a
--- vim80/src/ex_cmds.c.cve1785	2022-06-10 10:46:33.818286626 +0200
3e779a
+++ vim80/src/ex_cmds.c	2022-06-10 10:58:04.009515524 +0200
3e779a
@@ -5486,12 +5486,17 @@ do_sub(exarg_T *eap)
3e779a
 		/* Save flags for recursion.  They can change for e.g.
3e779a
 		 * :s/^/\=execute("s#^##gn") */
3e779a
 		subflags_save = subflags;
3e779a
+
3e779a
+		// Disallow changing text or switching window in an expression.
3e779a
+		++textlock;
3e779a
 #endif
3e779a
 		/* get length of substitution part */
3e779a
 		sublen = vim_regsub_multi(&regmatch,
3e779a
 				    sub_firstlnum - regmatch.startpos[0].lnum,
3e779a
 				    sub, sub_firstline, FALSE, p_magic, TRUE);
3e779a
 #ifdef FEAT_EVAL
3e779a
+		--textlock;
3e779a
+
3e779a
 		/* Don't keep flags set by a recursive call. */
3e779a
 		subflags = subflags_save;
3e779a
 		if (subflags.do_count)
3e779a
@@ -5570,9 +5575,15 @@ do_sub(exarg_T *eap)
3e779a
 		mch_memmove(new_end, sub_firstline + copycol, (size_t)copy_len);
3e779a
 		new_end += copy_len;
3e779a
 
3e779a
+#ifdef FEAT_EVAL
3e779a
+		++textlock;
3e779a
+#endif
3e779a
 		(void)vim_regsub_multi(&regmatch,
3e779a
 				    sub_firstlnum - regmatch.startpos[0].lnum,
3e779a
 					   sub, new_end, TRUE, p_magic, TRUE);
3e779a
+#ifdef FEAT_EVAL
3e779a
+		--textlock;
3e779a
+#endif
3e779a
 		sub_nsubs++;
3e779a
 		did_sub = TRUE;
3e779a
 
3e779a
diff -up vim80/src/testdir/test_substitute.vim.cve1785 vim80/src/testdir/test_substitute.vim
3e779a
--- vim80/src/testdir/test_substitute.vim.cve1785	2022-06-10 10:46:33.818286626 +0200
3e779a
+++ vim80/src/testdir/test_substitute.vim	2022-06-10 10:59:17.168437630 +0200
3e779a
@@ -500,3 +500,16 @@ func Test_sub_cmd_8()
3e779a
   enew!
3e779a
   set titlestring&
3e779a
 endfunc
3e779a
+
3e779a
+" This was switching windows in between computing the length and using it.
3e779a
+func Test_sub_change_window()
3e779a
+  silent! lfile
3e779a
+  sil! norm o0000000000000000000000000000000000000000000000000000
3e779a
+  func Repl()
3e779a
+    lopen
3e779a
+  endfunc
3e779a
+  silent!  s/\%')/\=Repl()
3e779a
+  bwipe!
3e779a
+  bwipe!
3e779a
+  delfunc Repl
3e779a
+endfunc