In perl.git, the branch blead has been updated <http://perl5.git.perl.org/perl.git/commitdiff/434f489be7a3ec92b6d20b3474a9eee71a87e2a1?hp=01b5ef509f2ebf466fd7de2c1e7406717bb14332>
- Log ----------------------------------------------------------------- commit 434f489be7a3ec92b6d20b3474a9eee71a87e2a1 Author: Father Chrysostomos <[email protected]> Date: Sat Jun 8 00:11:40 2013 -0700 perldiag: typo M pod/perldiag.pod commit cb1ad50e615d49477a97593b9e0569e7167a258e Author: Father Chrysostomos <[email protected]> Date: Sat Jun 8 00:11:23 2013 -0700 [perl #117941] eval crashes when current stash is freed M pp_ctl.c M t/op/eval.t commit dcdfe746ecfff81cc2793db4e7fdfe25be590e0b Author: Father Chrysostomos <[email protected]> Date: Fri Jun 7 23:56:23 2013 -0700 [perl #117941] Blessing into freed current stash If the current stash has been freed, bless() with one argument will cause a crash when the objectâs âstashâ is accessed. Simply disallow- ing this is the easiest fix. M pod/perldiag.pod M pp.c M t/op/bless.t commit 94e7eb6fe4b29b1c1dbe5eb489b4bfae607d9350 Author: Father Chrysostomos <[email protected]> Date: Fri Jun 7 23:41:32 2013 -0700 [perl #117941] Handle vivification crashing w/freed stash openâs handle vivification could crash if the current stash was freed, so check before passing a freed stash to gv_init. M pp.c M t/io/open.t commit 6cda7db16df9c75aa4f97b02443762d11e9bc150 Author: Father Chrysostomos <[email protected]> Date: Fri Jun 7 23:28:38 2013 -0700 Stop using PL_sortstash Originally, the value of PL_sortstash was used to determine whether PL_firstgv and PL_secondgv (which point to *a and *b in the current package) needed to be updated (involving symbol lookup). PL_sortstash would contain the last package sorted in. If PL_sortstash did not point to the current stash, then they would be updated, along with PL_sortstash. Sort was made reëntrant in 2000 in commit 8e664e1028b. This involved saving and restoring the values of PL_firstgv and PL_secondgv via the savestack. The logic introduced by that commit was thus: + if (PL_sortstash != stash || !PL_firstgv || !PL_secondgv) { + SAVESPTR(PL_firstgv); + SAVESPTR(PL_secondgv); + PL_firstgv = gv_fetchpv("a", TRUE, SVt_PV); + PL_secondgv = gv_fetchpv("b", TRUE, SVt_PV); + PL_sortstash = stash; + } PL_firstgv and PL_secondgv would be null if no sort were already hap- pening, causing this block to be executed. For a nested sort, this would only be executed if the nested sort were in a different package from the outer sort. Because the value of PL_sortstash was not being restored, this block would not trigger the second time the outer sort called the inner sort (in a different package). This was bug #36430 (a duplicate of #7063). This was fixed in commit 9850bf21fc4e, which did not explain anything in its commit message. A preliminary version of the patch was submit- ted for review in <[email protected]>, which mentions that the patch fixes bug #36430. It fixed the bug by localising the value of PL_sortstash, causing the if() condition to become redundant, so that was removed. Now, it happened that that if() condition was the only place that PL_sortstash was used. So if we are going to localise PL_firstgv and PL_secondgv unconditionally when sorting, PL_sortstash serves no purpose. While I could restore the if() condition and leave the localisation of PL_sortstash in place, that would only benefit code that does nested sort calls in the same package, which is rare, and the resulting speed-up is barely measurable. PL_sortstash is referenced by some CPAN modules, so I am taking the cautious route of leaving it in for now, though the core doesnât use it. M pp_sort.c M sv.c ----------------------------------------------------------------------- Summary of changes: pod/perldiag.pod | 8 +++++++- pp.c | 8 +++++++- pp_ctl.c | 4 +++- pp_sort.c | 2 -- sv.c | 1 - t/io/open.t | 7 ++++++- t/op/bless.t | 8 +++++++- t/op/eval.t | 7 ++++++- 8 files changed, 36 insertions(+), 9 deletions(-) diff --git a/pod/perldiag.pod b/pod/perldiag.pod index 86e1b46..a228d23 100644 --- a/pod/perldiag.pod +++ b/pod/perldiag.pod @@ -259,6 +259,12 @@ thread. See L<threads>. (F) The failing code has attempted to get or set a key which is not in the current set of allowed keys of a restricted hash. +=item Attempt to bless into a freed package + +(F) You wrote C<bless $foo> with one argument after somehow causing +the current package to be freed. Perl cannot figure out what to +do, so it throws up in hands in despair. + =item Attempt to bless into a reference (F) The CLASSNAME argument to the bless() operator is expected to be @@ -557,7 +563,7 @@ copiable. =item Bizarre SvTYPE [%d] -(P) When starting a new thread or return values from a thread, Perl +(P) When starting a new thread or returning values from a thread, Perl encountered an invalid data type. =item Buffer overflow in prime_env_iter: %s diff --git a/pp.c b/pp.c index 3eb3cea..e63c342 100644 --- a/pp.c +++ b/pp.c @@ -237,8 +237,10 @@ S_rv2gv(pTHX_ SV *sv, const bool vivify_sv, const bool strict, Perl_croak_no_modify(); if (cUNOP->op_targ) { SV * const namesv = PAD_SV(cUNOP->op_targ); + HV *stash = CopSTASH(PL_curcop); + if (SvTYPE(stash) != SVt_PVHV) stash = NULL; gv = MUTABLE_GV(newSV(0)); - gv_init_sv(gv, CopSTASH(PL_curcop), namesv, 0); + gv_init_sv(gv, stash, namesv, 0); } else { const char * const name = CopSTASHPV(PL_curcop); @@ -605,8 +607,12 @@ PP(pp_bless) HV *stash; if (MAXARG == 1) + { curstash: stash = CopSTASH(PL_curcop); + if (SvTYPE(stash) != SVt_PVHV) + Perl_croak(aTHX_ "Attempt to bless into a freed package"); + } else { SV * const ssv = POPs; STRLEN len; diff --git a/pp_ctl.c b/pp_ctl.c index b3e813e..7a2ba07 100644 --- a/pp_ctl.c +++ b/pp_ctl.c @@ -3402,7 +3402,9 @@ S_doeval(pTHX_ int gimme, CV* outside, U32 seq, HV *hh) if (CopSTASH_ne(PL_curcop, PL_curstash)) { SAVEGENERICSV(PL_curstash); - PL_curstash = (HV *)SvREFCNT_inc_simple(CopSTASH(PL_curcop)); + PL_curstash = (HV *)CopSTASH(PL_curcop); + if (SvTYPE(PL_curstash) != SVt_PVHV) PL_curstash = NULL; + else SvREFCNT_inc_simple_void(PL_curstash); } /* XXX:ajgo do we really need to alloc an AV for begin/checkunit */ SAVESPTR(PL_beginav); diff --git a/pp_sort.c b/pp_sort.c index bf7182b..56c0aac 100644 --- a/pp_sort.c +++ b/pp_sort.c @@ -1648,10 +1648,8 @@ PP(pp_sort) if (!hasargs && !is_xsub) { SAVESPTR(PL_firstgv); SAVESPTR(PL_secondgv); - SAVESPTR(PL_sortstash); PL_firstgv = gv_fetchpvs("a", GV_ADD|GV_NOTQUAL, SVt_PV); PL_secondgv = gv_fetchpvs("b", GV_ADD|GV_NOTQUAL, SVt_PV); - PL_sortstash = stash; SAVESPTR(GvSV(PL_firstgv)); SAVESPTR(GvSV(PL_secondgv)); } diff --git a/sv.c b/sv.c index 8cd2cb2..bcc9a22 100644 --- a/sv.c +++ b/sv.c @@ -13653,7 +13653,6 @@ perl_clone_using(PerlInterpreter *proto_perl, UV flags, PL_errors = sv_dup_inc(proto_perl->Ierrors, param); PL_sortcop = (OP*)any_dup(proto_perl->Isortcop, proto_perl); - PL_sortstash = hv_dup(proto_perl->Isortstash, param); PL_firstgv = gv_dup(proto_perl->Ifirstgv, param); PL_secondgv = gv_dup(proto_perl->Isecondgv, param); diff --git a/t/io/open.t b/t/io/open.t index e06fc8e..ef56dda 100644 --- a/t/io/open.t +++ b/t/io/open.t @@ -10,7 +10,7 @@ $| = 1; use warnings; use Config; -plan tests => 121; +plan tests => 122; my $Perl = which_perl(); @@ -386,3 +386,8 @@ SKIP: { ok( eval { $fh->autoflush(1); 1 }, '$fh->autoflush(1) lives' ); ok( $INC{'IO/File.pm'}, "IO::File now loaded" ); } + +sub _117941 { package _117941; open my $a, "TEST" } +delete $::{"_117941::"}; +_117941(); +pass("no crash when open autovivifies glob in freed package"); diff --git a/t/op/bless.t b/t/op/bless.t index 7ed3d43..801e985 100644 --- a/t/op/bless.t +++ b/t/op/bless.t @@ -6,7 +6,7 @@ BEGIN { require './test.pl'; } -plan (109); +plan (110); sub expected { my($object, $package, $type) = @_; @@ -142,3 +142,9 @@ expected($c4, 'C4', "SCALAR"); bless [], "main::"; ok(1, 'blessing into main:: does not crash'); # [perl #87388] + +sub _117941 { package _117941; bless [] } +delete $::{"_117941::"}; +eval { _117941() }; +like $@, qr/^Attempt to bless into a freed package at /, + 'bless with one arg when current stash is freed'; diff --git a/t/op/eval.t b/t/op/eval.t index 49f7494..2c61a88 100644 --- a/t/op/eval.t +++ b/t/op/eval.t @@ -6,7 +6,7 @@ BEGIN { require './test.pl'; } -plan(tests => 128); +plan(tests => 129); eval 'pass();'; @@ -618,3 +618,8 @@ syntax error at (eval 1) line 1, at EOF EOE qq'Right line number for eval "$_"'; } + +sub _117941 { package _117941; eval '$a' } +delete $::{"_117941::"}; +_117941(); +pass("eval in freed package does not crash"); -- Perl5 Master Repository
