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

da4393
diff --git a/src/ops.c b/src/ops.c
da4393
index d8e96ff..88992b6 100644
da4393
--- a/src/ops.c
da4393
+++ b/src/ops.c
da4393
@@ -534,22 +534,27 @@ block_insert(
da4393
 	    if (b_insert)
da4393
 	    {
da4393
 		off = (*mb_head_off)(oldp, oldp + offset + spaces);
da4393
+		spaces -= off;
da4393
+		count -= off;
da4393
 	    }
da4393
 	    else
da4393
 	    {
da4393
-		off = (*mb_off_next)(oldp, oldp + offset);
da4393
-		offset += off;
da4393
+		// spaces fill the gap, the character that's at the edge moves
da4393
+		// right
da4393
+		off = (*mb_head_off)(oldp, oldp + offset);
da4393
+		offset -= off;
da4393
 	    }
da4393
-	    spaces -= off;
da4393
-	    count -= off;
da4393
 	}
da4393
 
da4393
-	newp = alloc(STRLEN(oldp) + s_len + count + 1);
da4393
+	// Make sure the allocated size matches what is actually copied below.
da4393
+	newp = alloc(STRLEN(oldp) + spaces + s_len
da4393
+		    + (spaces > 0 && !bdp->is_short ? ts_val - spaces : 0)
da4393
+								  + count + 1);
da4393
 	if (newp == NULL)
da4393
 	    continue;
da4393
 
da4393
 	// copy up to shifted part
da4393
-	mch_memmove(newp, oldp, (size_t)(offset));
da4393
+	mch_memmove(newp, oldp, (size_t)offset);
da4393
 	oldp += offset;
da4393
 
da4393
 	// insert pre-padding
da4393
@@ -560,14 +565,21 @@ block_insert(
da4393
 	mch_memmove(newp + startcol, s, (size_t)s_len);
da4393
 	offset += s_len;
da4393
 
da4393
-	if (spaces && !bdp->is_short)
da4393
+	if (spaces > 0 && !bdp->is_short)
da4393
 	{
da4393
-	    // insert post-padding
da4393
-	    vim_memset(newp + offset + spaces, ' ', (size_t)(ts_val - spaces));
da4393
-	    // We're splitting a TAB, don't copy it.
da4393
-	    oldp++;
da4393
-	    // We allowed for that TAB, remember this now
da4393
-	    count++;
da4393
+	    if (*oldp == TAB)
da4393
+	    {
da4393
+		// insert post-padding
da4393
+		vim_memset(newp + offset + spaces, ' ',
da4393
+						    (size_t)(ts_val - spaces));
da4393
+		// we're splitting a TAB, don't copy it
da4393
+		oldp++;
da4393
+		// We allowed for that TAB, remember this now
da4393
+		count++;
da4393
+	    }
da4393
+	    else
da4393
+		// Not a TAB, no extra spaces
da4393
+		count = spaces;
da4393
 	}
da4393
 
da4393
 	if (spaces > 0)
da4393
@@ -1574,7 +1586,7 @@ op_insert(oparg_T *oap, long count1)
da4393
 		oap->start_vcol = t;
da4393
 	    }
da4393
 	    else if (oap->op_type == OP_APPEND
da4393
-		      && oap->end.col + oap->end.coladd
da4393
+		      && oap->start.col + oap->start.coladd
da4393
 			>= curbuf->b_op_start_orig.col
da4393
 					      + curbuf->b_op_start_orig.coladd)
da4393
 	    {
da4393
diff --git a/src/testdir/test_visual.vim b/src/testdir/test_visual.vim
da4393
index 7c5f973..dc8e376 100644
da4393
--- a/src/testdir/test_visual.vim
da4393
+++ b/src/testdir/test_visual.vim
da4393
@@ -967,4 +967,13 @@ func Test_visual_put_in_block()
da4393
   bwipe!
da4393
 endfunc
da4393
 
da4393
+func Test_visual_block_append_invalid_char()
da4393
+  " this was going over the end of the line
da4393
+  new
da4393
+  call setline(1, ['	   let xxx', 'xxxxxˆ', 'xxxxxxxxxxx'])
da4393
+  exe "normal 0\<C-V>jjA-\<Esc>"
da4393
+  call assert_equal(['	-   let xxx', 'xxxxx   -ˆ', 'xxxxxxxx-xxx'], getline(1, 3))
da4393
+  bwipe!
da4393
+endfunc
da4393
+
da4393
 " vim: shiftwidth=2 sts=2 expandtab