vim-patch:8.2.5037: cursor position may be invalid after "0;" range (#18782)

Problem:    Cursor position may be invalid after "0;" range.
Solution:   Check the cursor position when it was set by ";" in the range.
4d97a565ae
This commit is contained in:
zeertzjq 2022-05-29 07:00:18 +08:00 committed by GitHub
parent 840b7576e9
commit 772ae3e656
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 7 deletions

View File

@ -2751,6 +2751,8 @@ int parse_cmd_address(exarg_T *eap, char **errormsg, bool silent)
{ {
int address_count = 1; int address_count = 1;
linenr_T lnum; linenr_T lnum;
bool need_check_cursor = false;
int ret = FAIL;
// Repeat for all ',' or ';' separated addresses. // Repeat for all ',' or ';' separated addresses.
for (;;) { for (;;) {
@ -2760,7 +2762,7 @@ int parse_cmd_address(exarg_T *eap, char **errormsg, bool silent)
lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent, lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
eap->addr_count == 0, address_count++); eap->addr_count == 0, address_count++);
if (eap->cmd == NULL) { // error detected if (eap->cmd == NULL) { // error detected
return FAIL; goto theend;
} }
if (lnum == MAXLNUM) { if (lnum == MAXLNUM) {
if (*eap->cmd == '%') { // '%' - all lines if (*eap->cmd == '%') { // '%' - all lines
@ -2799,14 +2801,14 @@ int parse_cmd_address(exarg_T *eap, char **errormsg, bool silent)
// there is no Vim command which uses '%' and // there is no Vim command which uses '%' and
// ADDR_WINDOWS or ADDR_TABS // ADDR_WINDOWS or ADDR_TABS
*errormsg = _(e_invrange); *errormsg = _(e_invrange);
return FAIL; goto theend;
} }
break; break;
case ADDR_TABS_RELATIVE: case ADDR_TABS_RELATIVE:
case ADDR_UNSIGNED: case ADDR_UNSIGNED:
case ADDR_QUICKFIX: case ADDR_QUICKFIX:
*errormsg = _(e_invrange); *errormsg = _(e_invrange);
return FAIL; goto theend;
case ADDR_ARGUMENTS: case ADDR_ARGUMENTS:
if (ARGCOUNT == 0) { if (ARGCOUNT == 0) {
eap->line1 = eap->line2 = 0; eap->line1 = eap->line2 = 0;
@ -2831,19 +2833,19 @@ int parse_cmd_address(exarg_T *eap, char **errormsg, bool silent)
// '*' - visual area // '*' - visual area
if (eap->addr_type != ADDR_LINES) { if (eap->addr_type != ADDR_LINES) {
*errormsg = _(e_invrange); *errormsg = _(e_invrange);
return FAIL; goto theend;
} }
eap->cmd++; eap->cmd++;
if (!eap->skip) { if (!eap->skip) {
pos_T *fp = getmark('<', false); pos_T *fp = getmark('<', false);
if (check_mark(fp) == FAIL) { if (check_mark(fp) == FAIL) {
return FAIL; goto theend;
} }
eap->line1 = fp->lnum; eap->line1 = fp->lnum;
fp = getmark('>', false); fp = getmark('>', false);
if (check_mark(fp) == FAIL) { if (check_mark(fp) == FAIL) {
return FAIL; goto theend;
} }
eap->line2 = fp->lnum; eap->line2 = fp->lnum;
eap->addr_count++; eap->addr_count++;
@ -2857,11 +2859,14 @@ int parse_cmd_address(exarg_T *eap, char **errormsg, bool silent)
if (*eap->cmd == ';') { if (*eap->cmd == ';') {
if (!eap->skip) { if (!eap->skip) {
curwin->w_cursor.lnum = eap->line2; curwin->w_cursor.lnum = eap->line2;
// Don't leave the cursor on an illegal line or column, but do // Don't leave the cursor on an illegal line or column, but do
// accept zero as address, so 0;/PATTERN/ works correctly. // accept zero as address, so 0;/PATTERN/ works correctly.
// Check the cursor position before returning.
if (eap->line2 > 0) { if (eap->line2 > 0) {
check_cursor(); check_cursor();
} }
need_check_cursor = true;
} }
} else if (*eap->cmd != ',') { } else if (*eap->cmd != ',') {
break; break;
@ -2877,7 +2882,13 @@ int parse_cmd_address(exarg_T *eap, char **errormsg, bool silent)
eap->addr_count = 0; eap->addr_count = 0;
} }
} }
return OK; ret = OK;
theend:
if (need_check_cursor) {
check_cursor();
}
return ret;
} }
/// Check for an Ex command with optional tail. /// Check for an Ex command with optional tail.

View File

@ -422,5 +422,13 @@ func Test_address_line_overflow()
bwipe! bwipe!
endfunc endfunc
" This was leaving the cursor in line zero
func Test_using_zero_in_range()
new
norm o00
silent! 0;s/\%')
bwipe!
endfunc
" vim: shiftwidth=2 sts=2 expandtab " vim: shiftwidth=2 sts=2 expandtab