b2938d
From ab307de390c3459badcc89b3d77542b5b871b2e8 Mon Sep 17 00:00:00 2001
b2938d
From: Richard Leach <richardleach@users.noreply.github.com>
b2938d
Date: Tue, 20 Oct 2020 18:16:38 +0100
b2938d
Subject: [PATCH 2/2] pp_split: add TonyC's stack-not-refcounted-suggestion and
b2938d
 tests
b2938d
MIME-Version: 1.0
b2938d
Content-Type: text/plain; charset=UTF-8
b2938d
Content-Transfer-Encoding: 8bit
b2938d
b2938d
Signed-off-by: Petr Písař <ppisar@redhat.com>
b2938d
---
b2938d
 pp.c         | 5 ++++-
b2938d
 t/op/split.t | 5 +++++
b2938d
 2 files changed, 9 insertions(+), 1 deletion(-)
b2938d
b2938d
diff --git a/pp.c b/pp.c
b2938d
index ce16c56e63..5b5e163011 100644
b2938d
--- a/pp.c
b2938d
+++ b/pp.c
b2938d
@@ -6034,6 +6034,9 @@ PP(pp_split)
b2938d
             oldsave = PL_savestack_ix;
b2938d
         }
b2938d
 
b2938d
+	/* Some defence against stack-not-refcounted bugs */
b2938d
+	(void)sv_2mortal(SvREFCNT_inc_simple_NN(ary));
b2938d
+
b2938d
 	if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
b2938d
 	    PUSHMARK(SP);
b2938d
 	    XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
b2938d
@@ -6356,7 +6359,7 @@ PP(pp_split)
b2938d
     }
b2938d
 
b2938d
     PUTBACK;
b2938d
-    LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
b2938d
+    LEAVE_SCOPE(oldsave);
b2938d
     SPAGAIN;
b2938d
     if (realarray) {
b2938d
         if (!mg) {
b2938d
diff --git a/t/op/split.t b/t/op/split.t
b2938d
index 1d78a45bde..7a321645ac 100644
b2938d
--- a/t/op/split.t
b2938d
+++ b/t/op/split.t
b2938d
@@ -703,3 +703,8 @@ fresh_perl_is('my @ary; @ary = split(/\w(?{ @ary[1000] = 1 })/, "abc");',
b2938d
 fresh_perl_is('my @ary; @ary = split(/\w(?{ undef @ary })/, "abc");',
b2938d
         '',{},'(@ary = split ...) survives an (undef @ary)');
b2938d
 
b2938d
+# check the (@ary = split) optimisation survives stack-not-refcounted bugs
b2938d
+fresh_perl_is('our @ary; @ary = split(/\w(?{ *ary = 0 })/, "abc");',
b2938d
+        '',{},'(@ary = split ...) survives @ary destruction via typeglob');
b2938d
+fresh_perl_is('my $ary = []; @$ary = split(/\w(?{ $ary = [] })/, "abc");',
b2938d
+        '',{},'(@ary = split ...) survives @ary destruction via reassignment');
b2938d
-- 
b2938d
2.25.4
b2938d