Blame SOURCES/0001-patch-8.2.4120-block-insert-goes-over-the-end-of-the.patch

345ccf
diff -up vim80/src/ops.c.cve0261 vim80/src/ops.c
345ccf
--- vim80/src/ops.c.cve0261	2022-01-26 14:30:27.475308323 +0100
345ccf
+++ vim80/src/ops.c	2022-01-26 14:34:16.650933713 +0100
345ccf
@@ -636,23 +636,30 @@ block_insert(
345ccf
 	    if (b_insert)
345ccf
 	    {
345ccf
 		off = (*mb_head_off)(oldp, oldp + offset + spaces);
345ccf
+		spaces -= off;
345ccf
+		count -= off;
345ccf
 	    }
345ccf
 	    else
345ccf
 	    {
345ccf
-		off = (*mb_off_next)(oldp, oldp + offset);
345ccf
-		offset += off;
345ccf
+		// spaces fill the gap, the character that's at the edge moves
345ccf
+		// right
345ccf
+		off = (*mb_head_off)(oldp, oldp + offset);
345ccf
+		offset -= off;
345ccf
 	    }
345ccf
 	    spaces -= off;
345ccf
 	    count -= off;
345ccf
 	}
345ccf
 #endif
345ccf
 
345ccf
-	newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
345ccf
+	// Make sure the allocated size matches what is actually copied below.
345ccf
+	newp = alloc(STRLEN(oldp) + spaces + s_len
345ccf
+		    + (spaces > 0 && !bdp->is_short ? p_ts - spaces : 0)
345ccf
+								  + count + 1);
345ccf
 	if (newp == NULL)
345ccf
 	    continue;
345ccf
 
345ccf
 	/* copy up to shifted part */
345ccf
-	mch_memmove(newp, oldp, (size_t)(offset));
345ccf
+	mch_memmove(newp, oldp, (size_t)offset);
345ccf
 	oldp += offset;
345ccf
 
345ccf
 	/* insert pre-padding */
345ccf
@@ -662,14 +669,21 @@ block_insert(
345ccf
 	mch_memmove(newp + offset + spaces, s, (size_t)s_len);
345ccf
 	offset += s_len;
345ccf
 
345ccf
-	if (spaces && !bdp->is_short)
345ccf
+	if (spaces > 0 && !bdp->is_short)
345ccf
 	{
345ccf
-	    /* insert post-padding */
345ccf
-	    vim_memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces));
345ccf
-	    /* We're splitting a TAB, don't copy it. */
345ccf
-	    oldp++;
345ccf
-	    /* We allowed for that TAB, remember this now */
345ccf
-	    count++;
345ccf
+	    if (*oldp == TAB)
345ccf
+	    {
345ccf
+		// insert post-padding
345ccf
+		vim_memset(newp + offset + spaces, ' ',
345ccf
+						    (size_t)(p_ts - spaces));
345ccf
+		// we're splitting a TAB, don't copy it
345ccf
+		oldp++;
345ccf
+		// We allowed for that TAB, remember this now
345ccf
+		count++;
345ccf
+	    }
345ccf
+	    else
345ccf
+		// Not a TAB, no extra spaces
345ccf
+		count = spaces;
345ccf
 	}
345ccf
 
345ccf
 	if (spaces > 0)
345ccf
@@ -2738,9 +2752,9 @@ op_insert(oparg_T *oap, long count1)
345ccf
 		oap->start_vcol = t;
345ccf
 	    }
345ccf
 	    else if (oap->op_type == OP_APPEND
345ccf
-		      && oap->end.col
345ccf
+		      && oap->start.col
345ccf
 #ifdef FEAT_VIRTUALEDIT
345ccf
-			    + oap->end.coladd
345ccf
+			    + oap->start.coladd
345ccf
 #endif
345ccf
 			>= curbuf->b_op_start_orig.col
345ccf
 #ifdef FEAT_VIRTUALEDIT
345ccf
diff -up vim80/src/testdir/test_visual.vim.cve0261 vim80/src/testdir/test_visual.vim
345ccf
--- vim80/src/testdir/test_visual.vim.cve0261	2022-01-26 14:30:27.476308325 +0100
345ccf
+++ vim80/src/testdir/test_visual.vim	2022-01-26 14:36:03.482225225 +0100
345ccf
@@ -254,3 +254,12 @@ func Test_virtual_replace2()
345ccf
   %d_
345ccf
   set bs&vim
345ccf
 endfunc
345ccf
+
345ccf
+func Test_visual_block_append_invalid_char()
345ccf
+  " this was going over the end of the line
345ccf
+  new
345ccf
+  call setline(1, ['	   let xxx', 'xxxxxˆ', 'xxxxxxxxxxx'])
345ccf
+  exe "normal 0\<C-V>jjA-\<Esc>"
345ccf
+  call assert_equal(['	-   let xxx', 'xxxxx   -ˆ', 'xxxxxxxx-xxx'], getline(1, 3))
345ccf
+  bwipe!
345ccf
+endfunc