Skip to content
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

Fix stringified IV_MIN sometimes unintentionally treated as NV #22171

Merged
merged 1 commit into from
Jun 12, 2024
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
9 changes: 2 additions & 7 deletions sv.c
Original file line number Diff line number Diff line change
Expand Up @@ -2658,19 +2658,14 @@ Perl_sv_2nv_flags(pTHX_ SV *const sv, const I32 flags)
SvNOK_on(sv);
} else {
/* value has been set. It may not be precise. */
if ((numtype & IS_NUMBER_NEG) && (value >= (UV)IV_MIN)) {
/* 2s complement assumption for (UV)IV_MIN */
if ((numtype & IS_NUMBER_NEG) && (value > ABS_IV_MIN)) {
SvNOK_on(sv); /* Integer is too negative. */
} else {
SvNOKp_on(sv);
SvIOKp_on(sv);

if (numtype & IS_NUMBER_NEG) {
/* -IV_MIN is undefined, but we should never reach
* this point with both IS_NUMBER_NEG and value ==
* (UV)IV_MIN */
assert(value != (UV)IV_MIN);
SvIV_set(sv, -(IV)value);
SvIV_set(sv, NEGATE_2IV(value));
} else if (value <= (UV)IV_MAX) {
SvIV_set(sv, (IV)value);
} else {
Expand Down
15 changes: 15 additions & 0 deletions t/op/numconvert.t
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,18 @@ sub plus_one { (shift) + 1 }

is 0x1p60 - 1.0, 0x1p60 - 1,
"(0x1p60 - 1) and (0x1p60 - 1.0) should be identical";

my @iv_min_test;
BEGIN {
@iv_min_test = ('2147483647', '2147483648',
'9223372036854775807', '9223372036854775808');
$::additional_tests += @iv_min_test;
}

foreach my $str (@iv_min_test) {
my $x = "-$str";
my $y = $x + 1.23;
my $z = $x + 0;
my $w = "-$str" + 0;
is $z, $w, "previously NV-ified '-$str' has correct numeric value";
}
Loading