Skip to content

Commit 14d26b4

Browse files
committed
(perl #134230) don't interpret 0x, 0b when numifying strings
1 parent 6e0fc90 commit 14d26b4

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

numeric.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,6 +1551,15 @@ Perl_my_atof3(pTHX_ const char* orig, NV* value, const STRLEN len)
15511551
if ((endp = S_my_atof_infnan(aTHX_ s, negative, send, value)))
15521552
return endp;
15531553

1554+
/* strtold() accepts 0x-prefixed hex and in POSIX implementations,
1555+
0b-prefixed binary numbers, which is backward incompatible
1556+
*/
1557+
if ((len == 0 || len >= 2) && *s == '0' &&
1558+
(isALPHA_FOLD_EQ(s[1], 'x') || isALPHA_FOLD_EQ(s[1], 'b'))) {
1559+
*value = 0;
1560+
return (char *)s+1;
1561+
}
1562+
15541563
/* If the length is passed in, the input string isn't NUL-terminated,
15551564
* and in it turns out the function below assumes it is; therefore we
15561565
* create a copy and NUL-terminate that */

t/op/int.t

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ BEGIN {
77
require Config;
88
}
99

10-
plan 17;
10+
plan 19;
1111

1212
# compile time evaluation
1313

@@ -83,3 +83,6 @@ SKIP:
8383
cmp_ok($x, "==", int($x), "check $x == int($x)");
8484
}
8585
}
86+
87+
is(1+"0x10", 1, "check string '0x' prefix not treated as hex");
88+
is(1+"0b10", 1, "check string '0b' prefix not treated as binary");

0 commit comments

Comments
 (0)