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