Skip to content

Commit

Permalink
patch 8.1.0910: crash with tricky search pattern
Browse files Browse the repository at this point in the history
Problem:    Crash with tricky search pattern. (Kuang-che Wu)
Solution:   Check for runnning out of memory. (closes #3950)
  • Loading branch information
brammool committed Feb 13, 2019
1 parent d9ef1b8 commit 15bbd6e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/regexp_nfa.c
Expand Up @@ -4449,7 +4449,8 @@ addstate(
* be (a lot) bigger than anticipated. */
if (l->n == l->len)
{
int newlen = l->len * 3 / 2 + 50;
int newlen = l->len * 3 / 2 + 50;
nfa_thread_T *newt;

if (subs != &temp_subs)
{
Expand All @@ -4463,8 +4464,14 @@ addstate(
subs = &temp_subs;
}

/* TODO: check for vim_realloc() returning NULL. */
l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
newt = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
if (newt == NULL)
{
// out of memory
--depth;
return NULL;
}
l->t = newt;
l->len = newlen;
}

Expand Down Expand Up @@ -4756,7 +4763,7 @@ addstate_here(
* addstate(). */
r = addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET);
if (r == NULL)
return r;
return NULL;

// when "*ip" was at the end of the list, nothing to do
if (listidx + 1 == tlen)
Expand All @@ -4777,12 +4784,13 @@ addstate_here(
{
/* not enough space to move the new states, reallocate the list
* and move the states to the right position */
nfa_thread_T *newl;
int newlen = l->len * 3 / 2 + 50;
nfa_thread_T *newl;

l->len = l->len * 3 / 2 + 50;
newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
newl = (nfa_thread_T *)alloc(newlen * sizeof(nfa_thread_T));
if (newl == NULL)
return r;
return NULL;
l->len = newlen;
mch_memmove(&(newl[0]),
&(l->t[0]),
sizeof(nfa_thread_T) * listidx);
Expand Down
7 changes: 7 additions & 0 deletions src/testdir/test_regexp_latin.vim
Expand Up @@ -90,3 +90,10 @@ func Test_recursive_addstate()
let lnum = search('\v((){328}){389}')
call assert_equal(0, lnum)
endfunc

func Test_out_of_memory()
new
s/^/,n
" This will be slow...
call assert_fails('call search("\\v((n||<)+);")', 'E363:')
endfunc
2 changes: 2 additions & 0 deletions src/version.c
Expand Up @@ -783,6 +783,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
910,
/**/
909,
/**/
Expand Down

0 comments on commit 15bbd6e

Please sign in to comment.