Skip to content

Commit a7a691c

Browse files
committed
patch 8.2.2121: internal error when using \ze before \zs in a pattern
Problem: Internal error when using \ze before \zs in a pattern. Solution: Check the end is never before the start. (closes #7442)
1 parent 730677a commit a7a691c

4 files changed

Lines changed: 43 additions & 0 deletions

File tree

src/regexp_bt.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4805,6 +4805,23 @@ bt_regexec_both(
48054805
if (backpos.ga_maxlen > BACKPOS_INITIAL)
48064806
ga_clear(&backpos);
48074807

4808+
// Make sure the end is never before the start. Can happen when \zs and
4809+
// \ze are used.
4810+
if (REG_MULTI)
4811+
{
4812+
lpos_T *start = &rex.reg_mmatch->startpos[0];
4813+
lpos_T *end = &rex.reg_mmatch->endpos[0];
4814+
4815+
if (end->lnum < start->lnum
4816+
|| (end->lnum == start->lnum && end->col < start->col))
4817+
rex.reg_mmatch->endpos[0] = rex.reg_mmatch->startpos[0];
4818+
}
4819+
else
4820+
{
4821+
if (rex.reg_match->endp[0] < rex.reg_match->startp[0])
4822+
rex.reg_match->endp[0] = rex.reg_match->startp[0];
4823+
}
4824+
48084825
return retval;
48094826
}
48104827

src/regexp_nfa.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7225,6 +7225,23 @@ nfa_regexec_both(
72257225
#endif
72267226

72277227
theend:
7228+
// Make sure the end is never before the start. Can happen when \zs and
7229+
// \ze are used.
7230+
if (REG_MULTI)
7231+
{
7232+
lpos_T *start = &rex.reg_mmatch->startpos[0];
7233+
lpos_T *end = &rex.reg_mmatch->endpos[0];
7234+
7235+
if (end->lnum < start->lnum
7236+
|| (end->lnum == start->lnum && end->col < start->col))
7237+
rex.reg_mmatch->endpos[0] = rex.reg_mmatch->startpos[0];
7238+
}
7239+
else
7240+
{
7241+
if (rex.reg_match->endp[0] < rex.reg_match->startp[0])
7242+
rex.reg_match->endp[0] = rex.reg_match->startp[0];
7243+
}
7244+
72287245
return retval;
72297246
}
72307247

src/testdir/test_regexp_latin.vim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,13 @@ func Test_start_end_of_buffer_match()
911911
bwipe!
912912
endfunc
913913

914+
func Test_ze_before_zs()
915+
call assert_equal('', matchstr(' ', '\%#=1\ze \zs'))
916+
call assert_equal('', matchstr(' ', '\%#=2\ze \zs'))
917+
call assert_equal(repeat([''], 10), matchlist(' ', '\%#=1\ze \zs'))
918+
call assert_equal(repeat([''], 10), matchlist(' ', '\%#=2\ze \zs'))
919+
endfunc
920+
914921
" Check for detecting error
915922
func Test_regexp_error()
916923
call assert_fails("call matchlist('x x', '\\%#=1 \\zs*')", 'E888:')

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ static char *(features[]) =
750750

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2121,
753755
/**/
754756
2120,
755757
/**/

0 commit comments

Comments
 (0)