vim-patch:8.2.4120: block insert goes over the end of the line

Problem:    Block insert goes over the end of the line.
Solution:   Handle invalid byte better.  Fix inserting the wrong text.
9f8c304c8a
This commit is contained in:
Sean Dewar 2022-02-03 01:10:33 +00:00
parent f8b75e5822
commit e662d86e8d
No known key found for this signature in database
GPG Key ID: 08CC2C83AD41B581
2 changed files with 33 additions and 14 deletions

View File

@ -573,19 +573,24 @@ static void block_insert(oparg_T *oap, char_u *s, int b_insert, struct block_def
// Avoid starting halfway through a multi-byte character. // Avoid starting halfway through a multi-byte character.
if (b_insert) { if (b_insert) {
off = utf_head_off(oldp, oldp + offset + spaces); off = utf_head_off(oldp, oldp + offset + spaces);
spaces -= off;
count -= off;
} else { } else {
off = mb_off_next(oldp, oldp + offset); // spaces fill the gap, the character that's at the edge moves
offset += off; // right
off = utf_head_off(oldp, oldp + offset);
offset -= off;
} }
spaces -= off;
count -= off;
} }
if (spaces < 0) { // can happen when the cursor was moved if (spaces < 0) { // can happen when the cursor was moved
spaces = 0; spaces = 0;
} }
assert(count >= 0); assert(count >= 0);
newp = (char_u *)xmalloc(STRLEN(oldp) + s_len + (size_t)count + 1); // Make sure the allocated size matches what is actually copied below.
newp = xmalloc(STRLEN(oldp) + (size_t)spaces + s_len
+ (spaces > 0 && !bdp->is_short ? (size_t)p_ts - (size_t)spaces : 0)
+ (size_t)count + 1);
// copy up to shifted part // copy up to shifted part
memmove(newp, oldp, (size_t)offset); memmove(newp, oldp, (size_t)offset);
@ -600,14 +605,19 @@ static void block_insert(oparg_T *oap, char_u *s, int b_insert, struct block_def
offset += (int)s_len; offset += (int)s_len;
int skipped = 0; int skipped = 0;
if (spaces && !bdp->is_short) { if (spaces > 0 && !bdp->is_short) {
// insert post-padding if (*oldp == TAB) {
memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces)); // insert post-padding
// We're splitting a TAB, don't copy it. memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces));
oldp++; // We're splitting a TAB, don't copy it.
// We allowed for that TAB, remember this now oldp++;
count++; // We allowed for that TAB, remember this now
skipped = 1; count++;
skipped = 1;
} else {
// Not a TAB, no extra spaces
count = spaces;
}
} }
if (spaces > 0) { if (spaces > 0) {
@ -2329,7 +2339,7 @@ void op_insert(oparg_T *oap, long count1)
pre_textlen -= t - oap->start_vcol; pre_textlen -= t - oap->start_vcol;
oap->start_vcol = t; oap->start_vcol = t;
} else if (oap->op_type == OP_APPEND } else if (oap->op_type == OP_APPEND
&& oap->end.col + oap->end.coladd && oap->start.col + oap->start.coladd
>= curbuf->b_op_start_orig.col >= curbuf->b_op_start_orig.col
+ curbuf->b_op_start_orig.coladd) { + curbuf->b_op_start_orig.coladd) {
oap->start.col = curbuf->b_op_start_orig.col; oap->start.col = curbuf->b_op_start_orig.col;

View File

@ -1242,6 +1242,15 @@ func Test_visual_block_ctrl_w_f()
au! BufNew au! BufNew
endfunc endfunc
func Test_visual_block_append_invalid_char()
" this was going over the end of the line
new
call setline(1, [' let xxx', 'xxxxxˆ', 'xxxxxxxxxxx'])
exe "normal 0\<C-V>jjA-\<Esc>"
call assert_equal([' - let xxx', 'xxxxx -ˆ', 'xxxxxxxx-xxx'], getline(1, 3))
bwipe!
endfunc
func Test_visual_reselect_with_count() func Test_visual_reselect_with_count()
" this was causing an illegal memory access " this was causing an illegal memory access
let lines =<< trim END let lines =<< trim END