Fix assigning split() return values to an array

This commit is contained in:
Petr Písař 2016-12-01 07:52:54 +01:00
parent 8da6077616
commit 8fcd2745c8
2 changed files with 101 additions and 1 deletions

View File

@ -0,0 +1,94 @@
From 27a8a9e2a55ccc148582006396a9c35bafa5f0b3 Mon Sep 17 00:00:00 2001
From: David Mitchell <davem@iabyn.com>
Date: Wed, 30 Nov 2016 08:59:01 +0000
Subject: [PATCH] split was leaving PL_sv_undef in unused ary slots
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Petr Pisar: Ported to 5.24.0:
commit 71ca73e5fa9639ac33e9f2e74cd0c32288a5040d
Author: David Mitchell <davem@iabyn.com>
Date: Wed Nov 30 08:59:01 2016 +0000
split was leaving PL_sv_undef in unused ary slots
This:
@a = split(/-/,"-");
$a[1] = undef;
$a[0] = 0;
was giving
Modification of a read-only value attempted at foo line 3.
This is because:
1) unused slots in AvARRAY between AvFILL and AvMAX should always be
null; av_clear(), av_extend() etc do this; while av_store(), if storing
to a slot N somewhere between AvFILL and AvMAX, doesn't bother to clear
between (AvFILL+1)..(N-1) on the assumption that everyone else plays
nicely.
2) pp_split() when splitting directly to an array, sometimes over-splits
and has to null out the excess elements;
3) Since perl 5.19.4, unused AV slots are now marked with NULL rather than
&PL_sv_undef;
4) pp_split was still using &PL_sv_undef;
The fault was with (4), and is easily fixed.
Signed-off-by: Petr Písař <ppisar@redhat.com>
---
pp.c | 2 +-
t/op/split.t | 13 ++++++++++++-
2 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/pp.c b/pp.c
index 4153482..70345ce 100644
--- a/pp.c
+++ b/pp.c
@@ -6212,7 +6212,7 @@ PP(pp_split)
while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
if (TOPs && !make_mortal)
sv_2mortal(TOPs);
- *SP-- = &PL_sv_undef;
+ *SP-- = NULL;
iters--;
}
}
diff --git a/t/op/split.t b/t/op/split.t
index fb73271..b7846a1 100644
--- a/t/op/split.t
+++ b/t/op/split.t
@@ -7,7 +7,7 @@ BEGIN {
set_up_inc('../lib');
}
-plan tests => 131;
+plan tests => 133;
$FS = ':';
@@ -523,3 +523,14 @@ is "@a", '1 2 3', 'assignment to split-to-array (pmtarget/package array)';
}
(@{\@a} = split //, "abc") = 1..10;
is "@a", '1 2 3', 'assignment to split-to-array (stacked)';
+
+# splitting directly to an array wasn't filling unused AvARRAY slots with
+# NULL
+
+{
+ my @a;
+ @a = split(/-/,"-");
+ $a[1] = 'b';
+ ok eval { $a[0] = 'a'; 1; }, "array split filling AvARRAY: assign 0";
+ is "@a", "a b", "array split filling AvARRAY: result";
+}
--
2.7.4

View File

@ -227,6 +227,9 @@ Patch59: perl-5.25.7-Fix-Storable-segfaults.patch
# in upstream after 5.25.7
Patch60: perl-5.24.0-crash-on-explicit-return-from-s-e.patch
# Fix assigning split() return values to an array, in upstream after 5.25.7
Patch61: perl-5.24.0-split-was-leaving-PL_sv_undef-in-unused-ary-slots.patch
# Link XS modules to libperl.so with EU::CBuilder on Linux, bug #960048
Patch200: perl-5.16.3-Link-XS-modules-to-libperl.so-with-EU-CBuilder-on-Li.patch
@ -2913,6 +2916,7 @@ Perl extension for Version Objects
%patch58 -p1
%patch59 -p1
%patch60 -p1
%patch61 -p1
%patch200 -p1
%patch201 -p1
@ -2964,6 +2968,7 @@ perl -x patchlevel.h \
'Fedora Patch58: Fix stack handling when calling chdir without an argument (RT#129130)' \
'Fedora Patch59: Fix crash in Storable when deserializing malformed code reference (RT#68348, RT#130098)' \
'Fedora Patch60: Fix crash on explicit return from regular expression substitution (RT#130188)' \
'Fedora Patch61: Fix assigning split() return values to an array' \
'Fedora Patch200: Link XS modules to libperl.so with EU::CBuilder on Linux' \
'Fedora Patch201: Link XS modules to libperl.so with EU::MM on Linux' \
%{nil}
@ -5242,11 +5247,12 @@ popd
# Old changelog entries are preserved in CVS.
%changelog
* Mon Nov 28 2016 Petr Pisar <ppisar@redhat.com> - 4:5.24.0-381
* Thu Dec 01 2016 Petr Pisar <ppisar@redhat.com> - 4:5.24.0-381
- Fix crash in Storable when deserializing malformed code reference
(RT#68348, RT#130098)
- Fix crash on explicit return from regular expression substitution (RT#130188)
- Tighten dependencies between architecture specific sub-packages to ISA
- Fix assigning split() return values to an array
* Wed Nov 09 2016 Petr Pisar <ppisar@redhat.com> - 4:5.24.0-380
- Tie perl-Errno release to interpreter build because of kernel version check