Skip to content

Commit

Permalink
hash keys: validate hash key length at compile time
Browse files Browse the repository at this point in the history
This was new to me, I hadn't encountered this failure while working
on the original "other I32 bugs".

The original test here was failing with an "Out of memory" error
since the long hash key length was overflowing the I32.

Once that was fixed the test was failing purely due to the invalid
code, once that was fixed the test passed so I removed the TODO.
  • Loading branch information
tonycoz committed Jul 13, 2023
1 parent 6d66890 commit 77c06b8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
6 changes: 5 additions & 1 deletion op.c
Original file line number Diff line number Diff line change
Expand Up @@ -2742,7 +2742,11 @@ Perl_check_hash_fields_and_hekify(pTHX_ UNOP *rop, SVOP *key_op, int real)
{
SSize_t keylen;
const char * const key = SvPV_const(sv, *(STRLEN*)&keylen);
SV *nsv = newSVpvn_share(key, SvUTF8(sv) ? -keylen : keylen, 0);
if (keylen > I32_MAX) {
Perl_croak_nocontext("Sorry, hash keys must be smaller than 2**31 bytes");
}

SV *nsv = newSVpvn_share(key, SvUTF8(sv) ? -(I32)keylen : (I32)keylen, 0);
SvREFCNT_dec_NN(sv);
*svp = nsv;
}
Expand Down
9 changes: 3 additions & 6 deletions t/bigmem/hash.t
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ like(exn('my $h = { "x" x 2**31, undef }'),
qr/^\QSorry, hash keys must be smaller than 2**31 bytes\E\b/,
"hash constructed with huge key");

TODO: {
local $TODO = "Doesn't yet work with OP_MULTIDEREF";
like(exn('my %h; %h{ "x" x 2**31 } = undef'),
qr/^\QSorry, hash keys must be smaller than 2**31 bytes\E\b/,
"assign to huge hash key");
}
like(exn('my %h; $h{ "x" x 2**31 } = undef'),
qr/^\QSorry, hash keys must be smaller than 2**31 bytes\E\b/,
"assign to huge hash key");

0 comments on commit 77c06b8

Please sign in to comment.