b8876f
From b0254cedee2517d2705070839549189cf9f72db4 Mon Sep 17 00:00:00 2001
b8876f
From: David Mitchell <davem@iabyn.com>
b8876f
Date: Fri, 16 Jun 2017 15:46:19 +0100
b8876f
Subject: [PATCH] don't call Perl_fbm_instr() with negative length
b8876f
MIME-Version: 1.0
b8876f
Content-Type: text/plain; charset=UTF-8
b8876f
Content-Transfer-Encoding: 8bit
b8876f
b8876f
Ported to 5.24.1:
b8876f
b8876f
commit bb152a4b442f7718fd37d32cc558be675e8ae1ae
b8876f
Author: David Mitchell <davem@iabyn.com>
b8876f
Date:   Fri Jun 16 15:46:19 2017 +0100
b8876f
b8876f
    don't call Perl_fbm_instr() with negative length
b8876f
b8876f
    RT #131575
b8876f
b8876f
    re_intuit_start() could calculate a maximum end position less than the
b8876f
    current start position. This used to get rejected by fbm_intr(), until
b8876f
    v5.23.3-110-g147f21b, which made fbm_intr() faster and removed unnecessary
b8876f
    checks.
b8876f
b8876f
    This commits fixes re_intuit_start(), and adds an assert to  fbm_intr().
b8876f
b8876f
Signed-off-by: Petr Písař <ppisar@redhat.com>
b8876f
---
b8876f
 regexec.c  | 17 +++++++++++------
b8876f
 t/re/pat.t | 13 ++++++++++++-
b8876f
 util.c     |  2 ++
b8876f
 3 files changed, 25 insertions(+), 7 deletions(-)
b8876f
b8876f
diff --git a/regexec.c b/regexec.c
b8876f
index f1a52ab..3080880 100644
b8876f
--- a/regexec.c
b8876f
+++ b/regexec.c
b8876f
@@ -127,13 +127,16 @@ static const char* const non_utf8_target_but_utf8_required
b8876f
                     (U8*)(off >= 0 ? reginfo->strend : reginfo->strbeg)) \
b8876f
 	    : (U8*)(pos + off))
b8876f
 
b8876f
-#define HOPBACKc(pos, off) \
b8876f
-	(char*)(reginfo->is_utf8_target \
b8876f
-	    ? reghopmaybe3((U8*)pos, (SSize_t)0-off, (U8*)(reginfo->strbeg)) \
b8876f
-	    : (pos - off >= reginfo->strbeg)	\
b8876f
-		? (U8*)pos - off		\
b8876f
+/* like HOPMAYBE3 but backwards. lim must be +ve. Returns NULL on overshoot */
b8876f
+#define HOPBACK3(pos, off, lim) \
b8876f
+	(reginfo->is_utf8_target                          \
b8876f
+	    ? reghopmaybe3((U8*)pos, (SSize_t)0-off, (U8*)(lim)) \
b8876f
+	    : (pos - off >= lim)	                         \
b8876f
+		? (U8*)pos - off		                 \
b8876f
 		: NULL)
b8876f
 
b8876f
+#define HOPBACKc(pos, off) ((char*)HOPBACK3(pos, off, reginfo->strbeg))
b8876f
+
b8876f
 #define HOP3(pos,off,lim) (reginfo->is_utf8_target  ? reghop3((U8*)(pos), off, (U8*)(lim)) : (U8*)(pos + off))
b8876f
 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
b8876f
 
b8876f
@@ -871,7 +874,9 @@ Perl_re_intuit_start(pTHX_
b8876f
                 (IV)prog->check_end_shift);
b8876f
         });
b8876f
         
b8876f
-        end_point = HOP3(strend, -end_shift, strbeg);
b8876f
+        end_point = HOPBACK3(strend, end_shift, rx_origin);
b8876f
+        if (!end_point)
b8876f
+            goto fail_finish;
b8876f
         start_point = HOPMAYBE3(rx_origin, start_shift, end_point);
b8876f
         if (!start_point)
b8876f
             goto fail_finish;
b8876f
diff --git a/t/re/pat.t b/t/re/pat.t
b8876f
index 50529b8..007f11d 100644
b8876f
--- a/t/re/pat.t
b8876f
+++ b/t/re/pat.t
b8876f
@@ -23,7 +23,7 @@ BEGIN {
b8876f
     skip_all_without_unicode_tables();
b8876f
 }
b8876f
 
b8876f
-plan tests => 793;  # Update this when adding/deleting tests.
b8876f
+plan tests => 794;  # Update this when adding/deleting tests.
b8876f
 
b8876f
 run_tests() unless caller;
b8876f
 
b8876f
@@ -1783,6 +1783,17 @@ EOP
b8876f
         # [perl #129281] buffer write overflow, detected by ASAN, valgrind
b8876f
         fresh_perl_is('/0(?0)|^*0(?0)|^*(^*())0|/', '', {}, "don't bump whilem_c too much");
b8876f
     }
b8876f
+
b8876f
+    {
b8876f
+        # RT #131575 intuit skipping back from the end to find the highest
b8876f
+        # possible start point, was potentially hopping back beyond pos()
b8876f
+        # and crashing by calling fbm_instr with a negative length
b8876f
+
b8876f
+        my $text = "=t=\x{5000}";
b8876f
+        pos($text) = 3;
b8876f
+        ok(scalar($text !~ m{(~*=[a-z]=)}g), "RT #131575");
b8876f
+    }
b8876f
+
b8876f
 } # End of sub run_tests
b8876f
 
b8876f
 1;
b8876f
diff --git a/util.c b/util.c
b8876f
index df75db0..bc265f5 100644
b8876f
--- a/util.c
b8876f
+++ b/util.c
b8876f
@@ -806,6 +806,8 @@ Perl_fbm_instr(pTHX_ unsigned char *big, unsigned char *bigend, SV *littlestr, U
b8876f
 
b8876f
     PERL_ARGS_ASSERT_FBM_INSTR;
b8876f
 
b8876f
+    assert(bigend >= big);
b8876f
+
b8876f
     if ((STRLEN)(bigend - big) < littlelen) {
b8876f
 	if ( SvTAIL(littlestr)
b8876f
 	     && ((STRLEN)(bigend - big) == littlelen - 1)
b8876f
-- 
b8876f
2.9.4
b8876f