Skip to content

Commit 4ce9781

Browse files
committed
Fix parsing of large hex floats containing "e"
These ended up taking the code path for normal floats and being cast to zero.
1 parent f077f76 commit 4ce9781

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

lib/PhpParser/Node/Scalar/Float_.php

+7-11
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,7 @@ public static function fromString(string $str, array $attributes = []): Float_ {
4545
public static function parse(string $str): float {
4646
$str = str_replace('_', '', $str);
4747

48-
// if string contains any of .eE just cast it to float
49-
if (false !== strpbrk($str, '.eE')) {
50-
return (float) $str;
51-
}
52-
53-
// otherwise it's an integer notation that overflowed into a float
54-
// if it starts with 0 it's one of the special integer notations
48+
// Check whether this is one of the special integer notations.
5549
if ('0' === $str[0]) {
5650
// hex
5751
if ('x' === $str[1] || 'X' === $str[1]) {
@@ -63,10 +57,12 @@ public static function parse(string $str): float {
6357
return bindec($str);
6458
}
6559

66-
// oct
67-
// substr($str, 0, strcspn($str, '89')) cuts the string at the first invalid digit (8 or 9)
68-
// so that only the digits before that are used
69-
return octdec(substr($str, 0, strcspn($str, '89')));
60+
// oct, but only if the string does not contain any of '.eE'.
61+
if (false === strpbrk($str, '.eE')) {
62+
// substr($str, 0, strcspn($str, '89')) cuts the string at the first invalid digit
63+
// (8 or 9) so that only the digits before that are used.
64+
return octdec(substr($str, 0, strcspn($str, '89')));
65+
}
7066
}
7167

7268
// dec

test/code/parser/scalar/float.test

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Different float syntaxes
1717
// (all are actually the same number, just in different representations)
1818
18446744073709551615;
1919
0xFFFFFFFFFFFFFFFF;
20+
0xEEEEEEEEEEEEEEEE;
2021
01777777777777777777777;
2122
0177777777777777777777787;
2223
0b1111111111111111111111111111111111111111111111111111111111111111;
@@ -92,7 +93,7 @@ array(
9293
)
9394
12: Stmt_Expression(
9495
expr: Scalar_Float(
95-
value: 1.844674407371E+19
96+
value: 1.7216961135462E+19
9697
)
9798
)
9899
13: Stmt_Expression(
@@ -105,4 +106,9 @@ array(
105106
value: 1.844674407371E+19
106107
)
107108
)
108-
)
109+
15: Stmt_Expression(
110+
expr: Scalar_Float(
111+
value: 1.844674407371E+19
112+
)
113+
)
114+
)

0 commit comments

Comments
 (0)