From 618a07e8131f49bbda77d2cfe5832218a561a5c8 Mon Sep 17 00:00:00 2001 From: Paulo Cesar Pereira de Andrade Date: Tue, 1 Jul 2008 18:17:44 -0300 Subject: Fix a bug in the regex library The bug causes the regex parser to enter an infinite loop with certain special patterns with alternatives. Test cases also added to ensure the bug will be triggered by the tests if it is somehow reinstantiated. Also testing commit to xedit git repository. --- lisp/re/re.c | 3 ++- lisp/re/tests.c | 2 +- lisp/re/tests.txt | 9 +++++++++ lisp/test/regex.lsp | 11 +++++++++++ 4 files changed, 23 insertions(+), 2 deletions(-) (limited to 'lisp') diff --git a/lisp/re/re.c b/lisp/re/re.c index abf5cc4..fca7e28 100644 --- a/lisp/re/re.c +++ b/lisp/re/re.c @@ -782,7 +782,8 @@ next_lcstl:; case Re_AltNext: bas = eng.off - 1; /* Check if matched and if it is a better match */ - if (eng.sv[eng.off] - eng.so[eng.off] < + if (eng.eo[eng.off] >= eng.so[eng.off] && + eng.sv[eng.off] - eng.so[eng.off] < eng.eo[eng.off] - eng.so[eng.off]) eng.sv[eng.off] = eng.eo[eng.off]; diff --git a/lisp/re/tests.c b/lisp/re/tests.c index 6a82d41..21b1e10 100644 --- a/lisp/re/tests.c +++ b/lisp/re/tests.c @@ -164,7 +164,7 @@ main(int argc, char *argv[]) else { if (failed) { reerror(failed, &cod, buf, sizeof(buf)); - fprintf(stderr, "%s, at line %d\n", line); + fprintf(stderr, "%s, at line %d\n", buf, line); break; } if (sscanf(buf, "%ld,%ld:", &so, &eo) != 2) { diff --git a/lisp/re/tests.txt b/lisp/re/tests.txt index b8d3e22..35fd90b 100644 --- a/lisp/re/tests.txt +++ b/lisp/re/tests.txt @@ -459,3 +459,12 @@ /.*(\d+)/ :BADRPT + +# Regression fix, was matching empty string +/\\\d{3}|\\./ +>\\ +:NOMATCH + +/\\.|\\\d{3}/ +>\\ +:NOMATCH diff --git a/lisp/test/regex.lsp b/lisp/test/regex.lsp index fa6b2fe..9e28efa 100644 --- a/lisp/test/regex.lsp +++ b/lisp/test/regex.lsp @@ -438,3 +438,14 @@ foo" :notbol t :noteol t) (setq re (re-comp "(.*a)?")) (re-test '((0 . 1)) re "aaaa") ; expected, minimal match (re-test '((0 . 1) (0 . 1)) re "aaaa" :count 2) + + +;; Tue Dec 11 22:22:51 BRST 2007 Fix a regression with the pattern below +;; returning a match to an empty string. +;; Note that inverting the order of the "alternatives" works with the +;; versions of libre prior to this (one line) fix +(setq re (re-comp "\\\\\\d{3}|\\\\.")) +(re-test :nomatch re "\\") +;; previous version should work with the pattern inverted +(setq re (re-comp "\\\\.|\\\\\\d{3}")) +(re-test :nomatch re "\\") -- cgit v1.2.3