]> git.eshelyaron.com Git - emacs.git/commitdiff
regex.c (mutually_exclusive_p): Fix initial value of `loop_beg`
authorStefan Monnier <monnier@iro.umontreal.ca>
Tue, 26 Sep 2023 15:43:51 +0000 (11:43 -0400)
committerStefan Monnier <monnier@iro.umontreal.ca>
Tue, 26 Sep 2023 15:43:51 +0000 (11:43 -0400)
* src/regex-emacs.c (mutually_exclusive_p): Don't pretend that pattern
position 0 has been checked already.

* test/src/regex-emacs-tests.el (regexp-tests-backtrack-optimization):
Add a corresponding regression test plus some other related tests
I had around.

src/regex-emacs.c
test/src/regex-emacs-tests.el

index cf7b704ee95b53b5dda06cd728965fcda8164b42..338323cf79e5e5324bb8b2a43f59dd86dc235018 100644 (file)
@@ -3941,7 +3941,7 @@ static bool
 mutually_exclusive_p (struct re_pattern_buffer *bufp, re_char *p1,
                      re_char *p2)
 {
-  return mutually_exclusive_aux (bufp, p1, p2, bufp->buffer, NULL);
+  return mutually_exclusive_aux (bufp, p1, p2, bufp->buffer - 1, NULL);
 }
 \f
 /* Matching routines.  */
index f2bee7138641d5ebd65b8ba5421b7c69a8c4d66a..c84998e11845258c45404c5469f1a719b09d841f 100644 (file)
@@ -878,10 +878,28 @@ This evaluates the TESTS test cases from glibc."
     (erase-buffer)
     (insert (make-string 1000000 ?x) "=")
     (goto-char (point-min))
+    ;; Make sure we do perform the optimization (if we don't, the
+    ;; below will burp with regexp-stack-overflow).
     (should (looking-at "x*=*"))
     (should (looking-at "x*\\(=\\|:\\)"))
     (should (looking-at "x*\\(=\\|:\\)*"))
-    (should (looking-at "x*=*?"))))
+    (should (looking-at "x*=*?"))
+    (should (looking-at "x*\\(=*\\|h\\)*?"))
+    (should (looking-at "x*\\(=*\\|h\\)*"))
+    (should (looking-at "x*\\(=*?\\|h\\)*"))
+    (should (looking-at "x*\\(=*?\\|h\\)*?"))
+    (should (looking-at "x*\\(=*\\|h\\)+?"))
+    (should (looking-at "x*\\(=*\\|h\\)+"))
+    (should (looking-at "x*\\(=*?\\|h\\)+"))
+    (should (looking-at "x*\\(=*?\\|h\\)+?"))
+    (should (looking-at "x*\\(=+\\|h\\)+?"))
+    (should (looking-at "x*\\(=+\\|h\\)+"))
+    (should (looking-at "x*\\(=+?\\|h\\)+"))
+    (should (looking-at "x*\\(=+?\\|h\\)+?"))
+    ;; Regression check for overly optimistic optimization.
+    (should (eq 0 (string-match "\\(ca*\\|ab\\)+d" "cabd")))
+    (should (string-match "\\(aa*\\|b\\)*c" "ababc"))
+    ))
 
 (ert-deftest regexp-tests-zero-width-assertion-repetition ()
   ;; Check compatibility behaviour with repetition operators after