diff options
author | Bram Moolenaar <Bram@vim.org> | 2017-06-17 20:08:20 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2017-06-17 20:08:20 +0200 |
commit | 1ef9bbe215e13a273e74fccaddd8fc5a42c76b6e (patch) | |
tree | e7e91f215db3694732b431b3ff76eaa6008827d2 /src | |
parent | 5b1affefd0e96154517ec6f71300086ae6d22d24 (diff) |
patch 8.0.0645: no error for illegal back reference in NFA enginev8.0.0645
Problem: The new regexp engine does not give an error for using a back
reference where it is not allowed. (Dominique Pelle)
Solution: Check the back reference like the old engine. (closes #1774)
Diffstat (limited to 'src')
-rw-r--r-- | src/regexp.c | 48 | ||||
-rw-r--r-- | src/regexp_nfa.c | 10 | ||||
-rw-r--r-- | src/testdir/test_hlsearch.vim | 4 | ||||
-rw-r--r-- | src/testdir/test_regexp_latin.vim | 10 | ||||
-rw-r--r-- | src/testdir/test_statusline.vim | 2 | ||||
-rw-r--r-- | src/version.c | 2 |
6 files changed, 53 insertions, 23 deletions
diff --git a/src/regexp.c b/src/regexp.c index 74a73c4408..7304bd349b 100644 --- a/src/regexp.c +++ b/src/regexp.c @@ -1294,6 +1294,34 @@ skip_regexp( return p; } +/* + * Return TRUE if the back reference is legal. We must have seen the close + * brace. + * TODO: Should also check that we don't refer to something that is repeated + * (+*=): what instance of the repetition should we match? + */ + static int +seen_endbrace(int refnum) +{ + if (!had_endbrace[refnum]) + { + char_u *p; + + /* Trick: check if "@<=" or "@<!" follows, in which case + * the \1 can appear before the referenced match. */ + for (p = regparse; *p != NUL; ++p) + if (p[0] == '@' && p[1] == '<' && (p[2] == '!' || p[2] == '=')) + break; + if (*p == NUL) + { + EMSG(_("E65: Illegal back reference")); + rc_did_emsg = TRUE; + return FALSE; + } + } + return TRUE; +} + static regprog_T *bt_regcomp(char_u *expr, int re_flags); static void bt_regfree(regprog_T *prog); @@ -2099,24 +2127,8 @@ regatom(int *flagp) int refnum; refnum = c - Magic('0'); - /* - * Check if the back reference is legal. We must have seen the - * close brace. - * TODO: Should also check that we don't refer to something - * that is repeated (+*=): what instance of the repetition - * should we match? - */ - if (!had_endbrace[refnum]) - { - /* Trick: check if "@<=" or "@<!" follows, in which case - * the \1 can appear before the referenced match. */ - for (p = regparse; *p != NUL; ++p) - if (p[0] == '@' && p[1] == '<' - && (p[2] == '!' || p[2] == '=')) - break; - if (*p == NUL) - EMSG_RET_NULL(_("E65: Illegal back reference")); - } + if (!seen_endbrace(refnum)) + return NULL; ret = regnode(BACKREF + refnum); } break; diff --git a/src/regexp_nfa.c b/src/regexp_nfa.c index 5ba80f2fed..804d742ab3 100644 --- a/src/regexp_nfa.c +++ b/src/regexp_nfa.c @@ -1446,8 +1446,14 @@ nfa_regatom(void) case Magic('7'): case Magic('8'): case Magic('9'): - EMIT(NFA_BACKREF1 + (no_Magic(c) - '1')); - nfa_has_backref = TRUE; + { + int refnum = no_Magic(c) - '1'; + + if (!seen_endbrace(refnum + 1)) + return FAIL; + EMIT(NFA_BACKREF1 + refnum); + nfa_has_backref = TRUE; + } break; case Magic('z'): diff --git a/src/testdir/test_hlsearch.vim b/src/testdir/test_hlsearch.vim index b934d38e18..e9790bb9e5 100644 --- a/src/testdir/test_hlsearch.vim +++ b/src/testdir/test_hlsearch.vim @@ -38,11 +38,11 @@ func Test_hlsearch_hangs() return endif - " This pattern takes forever to match, it should timeout. + " This pattern takes a long time to match, it should timeout. help let start = reltime() set hlsearch nolazyredraw redrawtime=101 - let @/ = '\%#=2\v(a|\1)*' + let @/ = '\%#=1a*.*X\@<=b*' redraw let elapsed = reltimefloat(reltime(start)) call assert_true(elapsed > 0.1) diff --git a/src/testdir/test_regexp_latin.vim b/src/testdir/test_regexp_latin.vim index 247e5e65ab..a8fa64d2a7 100644 --- a/src/testdir/test_regexp_latin.vim +++ b/src/testdir/test_regexp_latin.vim @@ -62,3 +62,13 @@ func Test_eow_with_optional() call assert_equal(expected, actual) endfor endfunc + +func Test_backref() + new + call setline(1, ['one', 'two', 'three', 'four', 'five']) + call assert_equal(3, search('\%#=1\(e\)\1')) + call assert_equal(3, search('\%#=2\(e\)\1')) + call assert_fails('call search("\\%#=1\\(e\\1\\)")', 'E65:') + call assert_fails('call search("\\%#=2\\(e\\1\\)")', 'E65:') + bwipe! +endfunc diff --git a/src/testdir/test_statusline.vim b/src/testdir/test_statusline.vim index cf85bd58ac..351b119acd 100644 --- a/src/testdir/test_statusline.vim +++ b/src/testdir/test_statusline.vim @@ -223,7 +223,7 @@ func Test_statusline() set statusline=ab%(cd%q%)de call assert_match('^abde\s*$', s:get_statusline()) copen - call assert_match('^abcd\[Quickfix List\1]de\s*$', s:get_statusline()) + call assert_match('^abcd\[Quickfix List]de\s*$', s:get_statusline()) cclose " %#: Set highlight group. The name must follow and then a # again. diff --git a/src/version.c b/src/version.c index 24bb8206b4..2c3c45aed3 100644 --- a/src/version.c +++ b/src/version.c @@ -765,6 +765,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 645, +/**/ 644, /**/ 643, |