Blame SOURCES/0001-patch-8.2.4218-illegal-memory-access-with-bracketed-.patch

45b285
commit ec45bc7682fd698d8d39f43732129c4d092355f3
45b285
Author: Tomas Korbar <tkorbar@redhat.com>
45b285
Date:   Wed Feb 2 16:30:11 2022 +0100
45b285
45b285
    Fix illegal memory access with bracketed paste in Ex mode
45b285
45b285
diff --git a/src/edit.c b/src/edit.c
45b285
index f29fbc7..57b8dce 100644
45b285
--- a/src/edit.c
45b285
+++ b/src/edit.c
45b285
@@ -9519,27 +9519,33 @@ bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
45b285
     int		ret_char = -1;
45b285
     int		save_allow_keys = allow_keys;
45b285
     int		save_paste = p_paste;
45b285
-    int		save_ai = curbuf->b_p_ai;
45b285
 
45b285
-    /* If the end code is too long we can't detect it, read everything. */
45b285
-    if (STRLEN(end) >= NUMBUFLEN)
45b285
+    // If the end code is too long we can't detect it, read everything.
45b285
+    if (end != NULL && STRLEN(end) >= NUMBUFLEN)
45b285
 	end = NULL;
45b285
     ++no_mapping;
45b285
     allow_keys = 0;
45b285
-    p_paste = TRUE;
45b285
-    curbuf->b_p_ai = FALSE;
45b285
+    if (!p_paste)
45b285
+	// Also have the side effects of setting 'paste' to make it work much
45b285
+	// faster.
45b285
+	set_option_value((char_u *)"paste", TRUE, NULL, 0);
45b285
 
45b285
     for (;;)
45b285
     {
45b285
 	/* When the end is not defined read everything. */
45b285
 	if (end == NULL && vpeekc() == NUL)
45b285
 	    break;
45b285
-	c = plain_vgetc();
45b285
-#ifdef FEAT_MBYTE
45b285
+	do
45b285
+	    c = vgetc();
45b285
+	while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
45b285
+	if (c == NUL || got_int || (ex_normal_busy > 0 && c == Ctrl_C))
45b285
+	    // When CTRL-C was encountered the typeahead will be flushed and we
45b285
+	    // won't get the end sequence.  Except when using ":normal".
45b285
+	    break;
45b285
+
45b285
 	if (has_mbyte)
45b285
 	    idx += (*mb_char2bytes)(c, buf + idx);
45b285
 	else
45b285
-#endif
45b285
 	    buf[idx++] = c;
45b285
 	buf[idx] = NUL;
45b285
 	if (end != NULL && STRNCMP(buf, end, idx) == 0)
45b285
@@ -9557,7 +9563,8 @@ bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
45b285
 		    break;
45b285
 
45b285
 		case PASTE_EX:
45b285
-		    if (gap != NULL && ga_grow(gap, idx) == OK)
45b285
+		    // add one for the NUL that is going to be appended
45b285
+		    if (gap != NULL && ga_grow(gap, idx + 1) == OK)
45b285
 		    {
45b285
 			mch_memmove((char *)gap->ga_data + gap->ga_len,
45b285
 							     buf, (size_t)idx);
45b285
@@ -9582,11 +9589,9 @@ bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
45b285
 		case PASTE_ONE_CHAR:
45b285
 		    if (ret_char == -1)
45b285
 		    {
45b285
-#ifdef FEAT_MBYTE
45b285
 			if (has_mbyte)
45b285
 			    ret_char = (*mb_ptr2char)(buf);
45b285
 			else
45b285
-#endif
45b285
 			    ret_char = buf[0];
45b285
 		    }
45b285
 		    break;
45b285
@@ -9597,8 +9602,8 @@ bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
45b285
 
45b285
     --no_mapping;
45b285
     allow_keys = save_allow_keys;
45b285
-    p_paste = save_paste;
45b285
-    curbuf->b_p_ai = save_ai;
45b285
+    if (!save_paste)
45b285
+	set_option_value((char_u *)"paste", FALSE, NULL, 0);
45b285
 
45b285
     return ret_char;
45b285
 }