dcb3b7
From fbb9dc823a06b4815ee8fd8632fc475b8034e379 Mon Sep 17 00:00:00 2001
dcb3b7
From: Yves Orton <demerphq@gmail.com>
dcb3b7
Date: Fri, 27 Jan 2017 10:18:51 +0100
dcb3b7
Subject: [PATCH] fix RT #130561 - recursion and optimising away impossible
dcb3b7
 quantifiers are not friends
dcb3b7
MIME-Version: 1.0
dcb3b7
Content-Type: text/plain; charset=UTF-8
dcb3b7
Content-Transfer-Encoding: 8bit
dcb3b7
dcb3b7
Ported to 5.24.1:
dcb3b7
dcb3b7
commit 31fc93954d1f379c7a49889d91436ce99818e1f6
dcb3b7
Author: Yves Orton <demerphq@gmail.com>
dcb3b7
Date:   Fri Jan 27 10:18:51 2017 +0100
dcb3b7
dcb3b7
    fix RT #130561 - recursion and optimising away impossible quantifiers are not friends
dcb3b7
dcb3b7
    Instead of optimising away impossible quantifiers like (foo){1,0} treat them
dcb3b7
    as unquantified, and guard them with an OPFAIL. Thus /(foo){1,0}/ is treated
dcb3b7
    the same as /(*FAIL)(foo)/ this is important in patterns like /(foo){1,0}|(?1)/
dcb3b7
    where the (?1) needs to be able to recurse into the (foo) even though the
dcb3b7
    (foo){1,0} can never match. It also resolves various issues (SEGVs) with patterns
dcb3b7
    like /((?1)){1,0}/.
dcb3b7
dcb3b7
    This patch would have been easier if S_reginsert() documented that it is
dcb3b7
    the callers responsibility to properly set up the NEXT_OFF() of the inserted
dcb3b7
    node (if the node has a NEXT_OFF())
dcb3b7
dcb3b7
Signed-off-by: Petr Písař <ppisar@redhat.com>
dcb3b7
---
dcb3b7
 regcomp.c            | 14 +++-----------
dcb3b7
 t/re/pat_rt_report.t | 11 ++++++++++-
dcb3b7
 2 files changed, 13 insertions(+), 12 deletions(-)
dcb3b7
dcb3b7
diff --git a/regcomp.c b/regcomp.c
dcb3b7
index bcb8db5..9f343d3 100644
dcb3b7
--- a/regcomp.c
dcb3b7
+++ b/regcomp.c
dcb3b7
@@ -11497,19 +11497,11 @@ S_regpiece(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, U32 depth)
dcb3b7
 	    nextchar(pRExC_state);
dcb3b7
             if (max < min) {    /* If can't match, warn and optimize to fail
dcb3b7
                                    unconditionally */
dcb3b7
-                if (SIZE_ONLY) {
dcb3b7
-
dcb3b7
-                    /* We can't back off the size because we have to reserve
dcb3b7
-                     * enough space for all the things we are about to throw
dcb3b7
-                     * away, but we can shrink it by the amount we are about
dcb3b7
-                     * to re-use here */
dcb3b7
-                    RExC_size += PREVOPER(RExC_size) - regarglen[(U8)OPFAIL];
dcb3b7
-                }
dcb3b7
-                else {
dcb3b7
+                if (PASS2) {
dcb3b7
                     ckWARNreg(RExC_parse, "Quantifier {n,m} with n > m can't match");
dcb3b7
-                    RExC_emit = orig_emit;
dcb3b7
                 }
dcb3b7
-                ret = reganode(pRExC_state, OPFAIL, 0);
dcb3b7
+                reginsert(pRExC_state, OPFAIL, orig_emit, depth+1);
dcb3b7
+                NEXT_OFF(orig_emit)= regarglen[OPFAIL] + NODE_STEP_REGNODE;
dcb3b7
                 return ret;
dcb3b7
             }
dcb3b7
             else if (min == max && *RExC_parse == '?')
dcb3b7
diff --git a/t/re/pat_rt_report.t b/t/re/pat_rt_report.t
dcb3b7
index cb02ad2..2c1dbc4 100644
dcb3b7
--- a/t/re/pat_rt_report.t
dcb3b7
+++ b/t/re/pat_rt_report.t
dcb3b7
@@ -20,7 +20,7 @@ use warnings;
dcb3b7
 use 5.010;
dcb3b7
 use Config;
dcb3b7
 
dcb3b7
-plan tests => 2500;  # Update this when adding/deleting tests.
dcb3b7
+plan tests => 2502;  # Update this when adding/deleting tests.
dcb3b7
 
dcb3b7
 run_tests() unless caller;
dcb3b7
 
dcb3b7
@@ -1113,6 +1113,15 @@ EOP
dcb3b7
 	my $s = "\x{1ff}" . "f" x 32;
dcb3b7
 	ok($s =~ /\x{1ff}[[:alpha:]]+/gca, "POSIXA pointer wrap");
dcb3b7
     }
dcb3b7
+    {
dcb3b7
+        # rt
dcb3b7
+        fresh_perl_is(
dcb3b7
+            '"foo"=~/((?1)){8,0}/; print "ok"',
dcb3b7
+            "ok", {},  'RT #130561 - allowing impossible quantifier should not cause SEGVs');
dcb3b7
+        my $s= "foo";
dcb3b7
+        ok($s=~/(foo){1,0}|(?1)/,
dcb3b7
+            "RT #130561 - allowing impossible quantifier should not break recursion");
dcb3b7
+    }
dcb3b7
 } # End of sub run_tests
dcb3b7
 
dcb3b7
 1;
dcb3b7
-- 
dcb3b7
2.7.4
dcb3b7