[libxml2] Fix empty branch in regex



commit c2b0a184a9e052d445bedda817b233c05424062e
Author: Nick Wellnhofer <wellnhofer aevum de>
Date:   Wed Sep 25 13:57:42 2019 +0200

    Fix empty branch in regex
    
    Fixes bug 649244:
    https://bugzilla.gnome.org/show_bug.cgi?id=649244
    
    Closes #57.

 result/regexp/bug649244 |  9 +++++++++
 test/regexp/bug649244   |  9 +++++++++
 xmlregexp.c             | 14 +++++++-------
 3 files changed, 25 insertions(+), 7 deletions(-)
---
diff --git a/result/regexp/bug649244 b/result/regexp/bug649244
new file mode 100644
index 00000000..886ca2c8
--- /dev/null
+++ b/result/regexp/bug649244
@@ -0,0 +1,9 @@
+Regexp: S(a|)E
+SE: Ok
+SxE: Fail
+Regexp: S(|b)E
+SE: Ok
+SxE: Fail
+Regexp: S(a||b)E
+SE: Ok
+SxE: Fail
diff --git a/test/regexp/bug649244 b/test/regexp/bug649244
new file mode 100644
index 00000000..42988643
--- /dev/null
+++ b/test/regexp/bug649244
@@ -0,0 +1,9 @@
+=>S(a|)E
+SE
+SxE
+=>S(|b)E
+SE
+SxE
+=>S(a||b)E
+SE
+SxE
diff --git a/xmlregexp.c b/xmlregexp.c
index 10969177..7d4f63ee 100644
--- a/xmlregexp.c
+++ b/xmlregexp.c
@@ -5358,9 +5358,12 @@ xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) {
 
     previous = ctxt->state;
     ret = xmlFAParsePiece(ctxt);
-    if (ret != 0) {
+    if (ret == 0) {
+        /* Empty branch */
+       xmlFAGenerateEpsilonTransition(ctxt, previous, to);
+    } else {
        if (xmlFAGenerateTransitions(ctxt, previous,
-               (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
+               (CUR=='|' || CUR==')' || CUR==0) ? to : NULL, ctxt->atom) < 0)
            return(-1);
        previous = ctxt->state;
        ctxt->atom = NULL;
@@ -5369,7 +5372,8 @@ xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) {
        ret = xmlFAParsePiece(ctxt);
        if (ret != 0) {
            if (xmlFAGenerateTransitions(ctxt, previous,
-                   (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
+                   (CUR=='|' || CUR==')' || CUR==0) ? to : NULL,
+                    ctxt->atom) < 0)
                    return(-1);
            previous = ctxt->state;
            ctxt->atom = NULL;
@@ -5406,10 +5410,6 @@ xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
     end = ctxt->state;
     while ((CUR == '|') && (ctxt->error == 0)) {
        NEXT;
-       if (CUR == 0) {
-           ERROR("expecting a branch after |")
-           return;
-       }
        ctxt->state = start;
        ctxt->end = NULL;
        xmlFAParseBranch(ctxt, end);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]