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