-
Notifications
You must be signed in to change notification settings - Fork 560
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
chop(@x =~ tr///) #15738
Comments
From @hvdsCreated by @hvdsIn [perl #124368], Brian reports a second unrelated AFL-generated case % perl -e 'chop(@x =~ tr/1/1/)' Without -DDEBUGGING: % ./miniperl -wle '@x=(1..11); print scalar @x; print @x =~ tr/1/1/; print chop(@x =~ tr/1/1/)' The same works fine on a string, apparently happy to chop the temp scalar: % ./miniperl -wle '$s="11"; print $s; print $s =~ tr/1/1/; print chop($s =~ tr/1/1/)' .. but I note that if you throw a scalar() in there (which I'd have expected to have no effect), both cases complain (and in rather dodgy style): % ./miniperl -wle '@x=(1..11); chop(scalar(@x) =~ tr/1/1/)' I'm not sure now what correct behaviour would be here, or how to make it so. Perl Info
|
From @jkeenanOn Mon, 28 Nov 2016 11:32:38 GMT, hv wrote:
In 'perldoc -f tr', I see no documentation of anything like: ##### So if that is essentially undefined behavior, then, ##### would be undefined as well. -- |
The RT System itself - Status changed from 'new' to 'open' |
From @hvdsOn Fri, 30 Dec 2016 16:08:57 -0800, jkeenan wrote:
Well the docs say it expects to be supplied a string, and standard perl behaviours in such circumstances would be either to convert what is supplied into a string as best it can, or to give an error. Consider the analagous behaviour for a pattern match: % perl -wle '@x=(91..95); $y = (@x =~ /(\d+)/); print "$1 ($y)" if $y' Hugo |
From [Unknown Contact. See original ticket]On Fri, 30 Dec 2016 16:08:57 -0800, jkeenan wrote:
Well the docs say it expects to be supplied a string, and standard perl behaviours in such circumstances would be either to convert what is supplied into a string as best it can, or to give an error. Consider the analagous behaviour for a pattern match: % perl -wle '@x=(91..95); $y = (@x =~ /(\d+)/); print "$1 ($y)" if $y' Hugo |
From @iabynOn Sat, Dec 31, 2016 at 12:45:21AM -0800, Hugo van der Sanden via RT wrote:
This issue should be fixed by the following commit. commit 2108cbc Handle chop(@a =~ tr///) Affected files ... Differences ... Inline Patchdiff --git a/op.c b/op.c
index 394efef..339a9ce 100644
--- a/op.c
+++ b/op.c
@@ -3164,9 +3164,32 @@ Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
goto nomod;
else if (!(o->op_flags & OPf_KIDS))
break;
+
if (o->op_targ != OP_LIST) {
- op_lvalue(cBINOPo->op_first, type);
- break;
+ OP *sib = OpSIBLING(cLISTOPo->op_first);
+ /* OP_TRANS and OP_TRANSR with argument have a weird optree
+ * that looks like
+ *
+ * null
+ * arg
+ * trans
+ *
+ * compared with things like OP_MATCH which have the argument
+ * as a child:
+ *
+ * match
+ * arg
+ *
+ * so handle specially to correctly get "Can't modify" croaks etc
+ */
+
+ if (sib && (sib->op_type == OP_TRANS || sib->op_type == OP_TRANSR))
+ {
+ /* this should trigger a "Can't modify transliteration" err */
+ op_lvalue(sib, type);
+ }
+ op_lvalue(cBINOPo->op_first, type);
+ break;
}
/* FALLTHROUGH */
case OP_LIST:
diff --git a/t/op/tr.t b/t/op/tr.t
index 47acd9e..2ef2a68 100644
--- a/t/op/tr.t
+++ b/t/op/tr.t
@@ -13,7 +13,7 @@ BEGIN {
use utf8;
-plan tests => 166;
+plan tests => 214;
# Test this first before we extend the stack with other operations.
# This caused an asan failure due to a bad write past the end of the stack.
@@ -656,4 +656,48 @@ for ("", nullrocow) {
is($string, "A", 'tr// of \N{name} works for upper-Latin1');
}
+# RT #130198
+# a tr/// that is cho(m)ped, possibly with an array as arg
+
+{
+ use warnings;
+
+ my ($s, @a);
+
+ my $warn;
+ local $SIG{__WARN__ } = sub { $warn .= "@_" };
+
+ for my $c (qw(chop chomp)) {
+ for my $bind ('', '$s =~ ', '@a =~ ') {
+ for my $arg2 (qw(a b)) {
+ for my $r ('', 'r') {
+ $warn = '';
+ # tr/a/b/ modifies its LHS, so if the LHS is an
+ # array, this should die. The special cases of tr/a/a/
+ # and tr/a/b/r don't modify their LHS, so instead
+ # we croak because cho(m)p is trying to modify it.
+ #
+ my $exp =
+ ($r eq '' && $arg2 eq 'b' && $bind =~ /\@a/)
+ ? qr/Can't modify private array in transliteration/
+ : qr{Can't modify transliteration \(tr///\) in $c};
+
+ my $expr = "$c(${bind}tr/a/$arg2/$r);";
+ eval $expr;
+ like $@, $exp, "RT #130198 eval: $expr";
+
+ $exp =
+ $bind =~ /\@a/
+ ? qr{^Applying transliteration \(tr///\) to \@a will act on scalar\(\@a\)}
+ : qr/^$/;
+ like $warn, $exp, "RT #130198 warn: $expr";
+ }
+ }
+ }
+ }
+
+
+}
+
+
1;
-- Justice is when you get what you deserve. |
@iabyn - Status changed from 'open' to 'pending release' |
From @khwilliamsonThank you for filing this report. You have helped make Perl better. With the release today of Perl 5.26.0, this and 210 other issues have been Perl 5.26.0 may be downloaded via: If you find that the problem persists, feel free to reopen this ticket. |
@khwilliamson - Status changed from 'pending release' to 'resolved' |
Migrated from rt.perl.org#130198 (status was 'resolved')
Searchable as RT130198$
The text was updated successfully, but these errors were encountered: