Skip to content

Commit

Permalink
[perl #113486] pp_ctl.c: Don’t assume CopSTASH is a hash
Browse files Browse the repository at this point in the history
Calling HvNAME_HEK on something that is not a hash will result in a
crash if it happens to have the SvOOK flag on, because then it tries
to read to the end of HvARRAY, which may not even be a valid pointer.

This can happen with this convoluted test case:

{ package foo; sub bar { main::bar() } }
sub bar {
    delete $::{"foo::"};
    my $x = \($1+2);
    my $y = \($1+2); # this is the one that reuses the mem addr, but
    my $z = \($1+2);  # try the others just in case
    s/2// for $$x, $$y, $$z; # now SvOOK
    warn scalar caller
};
foo::bar

This commit only partially fixes ticket #113486, by eliminating
the crash.  We still have the problem of an unrelated stash reus-
ing the SV.
  • Loading branch information
Father Chrysostomos committed Jun 5, 2012
1 parent d0279c7 commit e788621
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 3 additions & 1 deletion pp_ctl.c
Expand Up @@ -1892,7 +1892,9 @@ PP(pp_caller)

DEBUG_CX("CALLER");
assert(CopSTASH(cx->blk_oldcop));
stash_hek = HvNAME_HEK((HV*)CopSTASH(cx->blk_oldcop));
stash_hek = SvTYPE(CopSTASH(cx->blk_oldcop)) == SVt_PVHV
? HvNAME_HEK((HV*)CopSTASH(cx->blk_oldcop))
: NULL;
if (GIMME != G_ARRAY) {
EXTEND(SP, 1);
if (!stash_hek)
Expand Down
18 changes: 17 additions & 1 deletion t/op/caller.t
Expand Up @@ -5,7 +5,7 @@ BEGIN {
chdir 't' if -d 't';
@INC = '../lib';
require './test.pl';
plan( tests => 85 );
plan( tests => 86 );
}

my @c;
Expand Down Expand Up @@ -250,6 +250,22 @@ eval { sub { () = caller 0; } ->(1..3) };
untie @args;
package main;

# [perl #113486]
fresh_perl_is <<'END', "ok\n", {},
{ package foo; sub bar { main::bar() } }
sub bar {
delete $::{"foo::"};
my $x = \($1+2);
my $y = \($1+2); # this is the one that reuses the mem addr, but
my $z = \($1+2); # try the others just in case
s/2// for $$x, $$y, $$z; # now SvOOK
$x = caller;
print "ok\n";
};
foo::bar
END
"No crash when freed stash is reused for PV with offset hack";

$::testing_caller = 1;

do './op/caller.pl' or die $@;

0 comments on commit e788621

Please sign in to comment.