Skip to content

Commit b62dc5e

Browse files
committed
patch 8.2.4959: using NULL regexp program
Problem: Using NULL regexp program. Solution: Check for regexp program becoming NULL in more places.
1 parent dd41037 commit b62dc5e

3 files changed

Lines changed: 30 additions & 13 deletions

File tree

src/buffer.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,13 +2642,15 @@ buflist_findpat(
26422642
if (*p == '^' && !(attempt & 1)) // add/remove '^'
26432643
++p;
26442644
regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
2645-
if (regmatch.regprog == NULL)
2646-
{
2647-
vim_free(pat);
2648-
return -1;
2649-
}
26502645

26512646
FOR_ALL_BUFS_FROM_LAST(buf)
2647+
{
2648+
if (regmatch.regprog == NULL)
2649+
{
2650+
// invalid pattern, possibly after switching engine
2651+
vim_free(pat);
2652+
return -1;
2653+
}
26522654
if (buf->b_p_bl == find_listed
26532655
#ifdef FEAT_DIFF
26542656
&& (!diffmode || diff_mode_buf(buf))
@@ -2674,6 +2676,7 @@ buflist_findpat(
26742676
}
26752677
match = buf->b_fnum; // remember first match
26762678
}
2679+
}
26772680

26782681
vim_regfree(regmatch.regprog);
26792682
if (match >= 0) // found one match
@@ -2766,12 +2769,6 @@ ExpandBufnames(
27662769
if (attempt > 0 && patc == pat)
27672770
break; // there was no anchor, no need to try again
27682771
regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2769-
if (regmatch.regprog == NULL)
2770-
{
2771-
if (patc != pat)
2772-
vim_free(patc);
2773-
return FAIL;
2774-
}
27752772
}
27762773

27772774
// round == 1: Count the matches.
@@ -2792,7 +2789,16 @@ ExpandBufnames(
27922789
#endif
27932790

27942791
if (!fuzzy)
2792+
{
2793+
if (regmatch.regprog == NULL)
2794+
{
2795+
// invalid pattern, possibly after recompiling
2796+
if (patc != pat)
2797+
vim_free(patc);
2798+
return FAIL;
2799+
}
27952800
p = buflist_match(&regmatch, buf, p_wic);
2801+
}
27962802
else
27972803
{
27982804
p = NULL;
@@ -2921,6 +2927,7 @@ ExpandBufnames(
29212927

29222928
/*
29232929
* Check for a match on the file name for buffer "buf" with regprog "prog".
2930+
* Note that rmp->regprog may become NULL when switching regexp engine.
29242931
*/
29252932
static char_u *
29262933
buflist_match(
@@ -2939,7 +2946,8 @@ buflist_match(
29392946
}
29402947

29412948
/*
2942-
* Try matching the regexp in "prog" with file name "name".
2949+
* Try matching the regexp in "rmp->regprog" with file name "name".
2950+
* Note that rmp->regprog may become NULL when switching regexp engine.
29432951
* Return "name" when there is a match, NULL when not.
29442952
*/
29452953
static char_u *
@@ -2951,7 +2959,8 @@ fname_match(
29512959
char_u *match = NULL;
29522960
char_u *p;
29532961

2954-
if (name != NULL)
2962+
// extra check for valid arguments
2963+
if (name != NULL && rmp->regprog != NULL)
29552964
{
29562965
// Ignore case when 'fileignorecase' or the argument is set.
29572966
rmp->rm_ic = p_fic || ignore_case;

src/testdir/test_buffer.vim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,12 @@ func Test_buf_pattern_invalid()
419419
vsplit 00000000000000000000000000
420420
silent! buf [0--]\&\zs*\zs*e
421421
bwipe!
422+
423+
" similar case with different code path
424+
split 0
425+
edit ÿ
426+
silent! buf [0--]\&\zs*\zs*0
427+
bwipe!
422428
endfunc
423429

424430
" Test for the 'maxmem' and 'maxmemtot' options

src/version.c

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

747747
static int included_patches[] =
748748
{ /* Add new patch number below this line */
749+
/**/
750+
4959,
749751
/**/
750752
4958,
751753
/**/

0 commit comments

Comments
 (0)