Skip to content

Commit

Permalink
[perl Perl#3112] Stop last from returning values
Browse files Browse the repository at this point in the history
In push @A, last, it can try to return the @A, copying it like a sca-
lar in the process, resulting in Bizarre copy of ARRAY in last.

In do{{&{sub{"Just another Perl hacker,\n"}},last}}, it returns "Just
another Perl hacker,\n".

The former is clearly a bug.  The latter depends on a side-effect of
the same bug.

‘last’ really should not be trying to return the values that the same
statement has accumulated so far.
  • Loading branch information
Father Chrysostomos committed Sep 20, 2013
1 parent 7497b91 commit 0c0c317
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
4 changes: 1 addition & 3 deletions pp_ctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2616,7 +2616,6 @@ PP(pp_last)

POPBLOCK(cx,newpm);
cxstack_ix++; /* temporarily protect top context */
mark = newsp;
switch (CxTYPE(cx)) {
case CXt_LOOP_LAZYIV:
case CXt_LOOP_LAZYSV:
Expand All @@ -2643,8 +2642,7 @@ PP(pp_last)
}

TAINT_NOT;
PL_stack_sp = adjust_stack_on_leave(newsp, PL_stack_sp, MARK, gimme,
pop2 == CXt_SUB ? SVs_TEMP : 0);
PL_stack_sp = newsp;

LEAVE;
cxstack_ix--;
Expand Down
18 changes: 16 additions & 2 deletions t/op/loopctl.t
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
BEGIN {
chdir 't' if -d 't';
@INC = qw(. ../lib);
require "test.pl";
}

require "test.pl";
plan( tests => 64 );
plan( tests => 67 );

my $ok;

Expand Down Expand Up @@ -1104,3 +1104,17 @@ redo_113684:
fail("redo with non-constant label");
}
}

# [perl #3112]
# The original report, which produced a Bizarre copy
@a = ();
eval {
for (1) {
push @a, last;
}
};
is @a, 0, 'push @a, last; does not push';
is $@, "", 'no error, either';
# And my japh, which relied on the misbehaviour
is do{{&{sub{"Just another Perl hacker,\n"}},last}}, undef,
'last returns nothing';

0 comments on commit 0c0c317

Please sign in to comment.