Skip to content

fix set magic handling for scalar reverse and an index() optimization #17740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions pp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3520,11 +3520,16 @@ PP(pp_index)
push_result:
/* OPpTRUEBOOL indicates an '== -1' has been optimised away */
if (PL_op->op_private & OPpTRUEBOOL) {
PUSHs( ((retval != -1) ^ cBOOL(PL_op->op_private & OPpINDEX_BOOLNEG))
? &PL_sv_yes : &PL_sv_no);
if (PL_op->op_private & OPpTARGET_MY)
SV *result = ((retval != -1) ^ cBOOL(PL_op->op_private & OPpINDEX_BOOLNEG))
? &PL_sv_yes : &PL_sv_no;
if (PL_op->op_private & OPpTARGET_MY) {
/* $lex = (index() == -1) */
sv_setsv(TARG, TOPs);
sv_setsv_mg(TARG, result);
PUSHs(TARG);
}
else {
PUSHs(result);
}
}
else
PUSHi(retval);
Expand Down Expand Up @@ -5898,6 +5903,7 @@ PP(pp_reverse)
sv_setsv(TARG, DEFSV);
XPUSHs(TARG);
}
SvSETMAGIC(TARG); /* remove any utf8 length magic */

up = SvPV_force(TARG, len);
if (len > 1) {
Expand Down
28 changes: 27 additions & 1 deletion t/op/index.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ BEGIN {
}

use strict;
plan( tests => 412 );
plan( tests => 414 );

run_tests() unless caller;

Expand Down Expand Up @@ -332,4 +332,30 @@ sub run_tests {

}

{
my $store = 100;
package MyTie {
require Tie::Scalar;
our @ISA = qw(Tie::StdScalar);
sub STORE {
my ($self, $value) = @_;

$store = $value;
}
};
my $x;
tie $x, "MyTie";
$x = (index("foo", "o") == -1);
ok(!$store, 'magic called on $lexical = (index(...) == -1)');
}
{
is(eval <<'EOS', "a", 'optimized $lex = (index(...) == -1) is an lvalue');
my $y = "foo";
my $z = "o";
my $x;
($x = (index($y, $z) == -1)) =~ s/^/a/;
$x;
EOS
}

} # end of sub run_tests
11 changes: 10 additions & 1 deletion t/op/reverse.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ BEGIN {
set_up_inc('../lib');
}

plan tests => 24;
plan tests => 25;

is(reverse("abc"), "cba", 'simple reverse');

Expand Down Expand Up @@ -92,6 +92,15 @@ use Tie::Array;
is($a, $c, 'Unicode string double reversal matches original');
}

{
# https://github.com/Perl/perl5/issues/17737
# utf8 length magic handling
# threw an assertion failure with -DDEBUGGING
my @x;
push @x, length reverse for split "-", "\x{100}--0";
is($x[1], 0, "check set magic being called to clear length magic");
}

# [perl #132544] stack pointer used to go wild when nullary reverse
# required extending the stack
for(0..1000){()=(0..$_,scalar reverse )}
Expand Down