Skip to content

Commit 8fa7f8e

Browse files
committed
Warn instead of DIE when a repetition would exhaust RAM
For discussions on #23561. perl -e 'use warnings; my $x = ($_) ? "A" x (2**62) : "Z"' gives this on blead for me: ``` Out of memory! panic: fold_constants JMPENV_PUSH returned 2 at -e line 1. ``` on the previous commit, it would die: ``` Unrealistically large string repetition value" ``` With this commit, it just warns: ``` Unrealistically large string repetition value at -e line 1. ``` but will blow up if the repetition OP does get executed: ``` Out of memory in perl:util:safesysrealloc ```
1 parent 3725af9 commit 8fa7f8e

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

op.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5025,20 +5025,19 @@ S_fold_constants(pTHX_ OP *const o)
50255025
if (SvIOKp(constsv)) {
50265026
if (SvIOK_UV(constsv)) {
50275027
if (SvUVX(constsv) > SIZE_MAX >> 2)
5028-
goto repetition_die;
5028+
ck_warner(packWARN(WARN_MISC), "Unrealistically large string repetition value");
50295029
if (SvUVX(constsv) > arbitrary)
50305030
goto nope;
50315031
} else {
50325032
if (SvIVX(constsv) > (IV)(SIZE_MAX >> 2))
5033-
goto repetition_die;
5033+
ck_warner(packWARN(WARN_MISC), "Unrealistically large string repetition value");
50345034
if (SvIVX(constsv) > (IV)arbitrary)
50355035
goto nope;
50365036
}
50375037
} else {
5038-
NV rhs = SvNV_nomg(constsv);
5039-
if (rhs > (NV)(SIZE_MAX >> 2)) {
5040-
repetition_die:
5041-
DIE(aTHX_ "Unrealistically large string repetition value");
5038+
NV rhs = 0.0; rhs = SvNV_nomg(constsv);
5039+
if (rhs >= (NV)((SIZE_MAX >> 2) +1) ) {
5040+
ck_warner(packWARN(WARN_MISC), "Unrealistically large string repetition value");
50425041
}
50435042
if (rhs > (NV)arbitrary)
50445043
goto nope;

pod/perldiag.pod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7387,7 +7387,7 @@ subroutine.
73877387

73887388
=item Unrealistically large string repetition value
73897389

7390-
The value of the right operand in the string repetition operator is
7390+
(W misc) The value of the right operand in the string repetition operator is
73917391
likely close to or will exceed the maximum memory allocation that
73927392
your system can provide.
73937393

t/op/repeat.t

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ is($@, "", "RT #130247");
195195

196196
# [GH #13324] Perl croaks if a string repetition seems unsupportable
197197
fresh_perl_like(
198-
'my $x = "A" x (2**99)',
198+
'use warnings; my $x = "A" x (2**99)',
199199
qr/Unrealistically large string repetition/,
200-
{ },
201-
'Croak on unrealistically large string repetition',
200+
{stderr => 1},
201+
'Warn on unrealistically large string repetition',
202202
);
203203

204204
# yes, the newlines matter

0 commit comments

Comments
 (0)