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