Skip to content

Commit a061ab0

Browse files
author
Father Chrysostomos
committed
[perl #128187] Forbid sub :lvalue{keys} in aassign
This commit makes perl die when keys(%hash) is returned from an lvalue sub and the lvalue sub call is assigned to in list assignment: sub foo : lvalue { keys(%INC) } (foo) = 3; # death This prevents an assignment that is completely useless and probably a mistake, and it makes the lvalue-sub use of keys behave the same way as (keys(%INC)) = 3.
1 parent 5ad9992 commit a061ab0

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

doop.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,13 @@ Perl_do_kv(pTHX)
12731273
RETURN;
12741274
}
12751275

1276+
if (UNLIKELY(PL_op->op_private & OPpMAYBE_LVSUB)) {
1277+
const I32 flags = is_lvalue_sub();
1278+
if (flags && !(flags & OPpENTERSUB_INARGS))
1279+
/* diag_listed_as: Can't modify %s in %s */
1280+
Perl_croak(aTHX_ "Can't modify keys in list assignment");
1281+
}
1282+
12761283
/* 2*HvUSEDKEYS() should never be big enough to truncate or wrap */
12771284
assert(HvUSEDKEYS(keys) <= (SSize_t_MAX >> 1));
12781285
extend_size = (SSize_t)HvUSEDKEYS(keys) * (dokeys + dovalues);

t/op/sub_lval.t

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ BEGIN {
55
@INC = '../lib';
66
require './test.pl';
77
}
8-
plan tests=>209;
8+
plan tests=>210;
99

1010
sub a : lvalue { my $a = 34; ${\(bless \$a)} } # Return a temporary
1111
sub b : lvalue { ${\shift} }
@@ -553,6 +553,9 @@ sub keeze : lvalue { keys %__ }
553553
%__ = ("a","b");
554554
keeze = 64;
555555
is scalar %__, '1/64', 'keys assignment through lvalue sub';
556+
eval { (keeze) = 64 };
557+
like $@, qr/^Can't modify keys in list assignment at /,
558+
'list assignment to keys through lv sub is forbidden';
556559

557560
# Bug 20001223.002: split thought that the list had only one element
558561
@ary = qw(4 5 6);

0 commit comments

Comments
 (0)