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

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