Skip to content

Commit

Permalink
Fix parsing of large hex floats containing "e"
Browse files Browse the repository at this point in the history
These ended up taking the code path for normal floats and being
cast to zero.

(cherry picked from commit 4ce9781)
  • Loading branch information
nikic committed Nov 12, 2022
1 parent 2f1fd78 commit 7027899
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
18 changes: 7 additions & 11 deletions lib/PhpParser/Node/Scalar/DNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,7 @@ public static function fromString(string $str, array $attributes = []): DNumber
public static function parse(string $str) : float {
$str = str_replace('_', '', $str);

// if string contains any of .eE just cast it to float
if (false !== strpbrk($str, '.eE')) {
return (float) $str;
}

// otherwise it's an integer notation that overflowed into a float
// if it starts with 0 it's one of the special integer notations
// Check whether this is one of the special integer notations.
if ('0' === $str[0]) {
// hex
if ('x' === $str[1] || 'X' === $str[1]) {
Expand All @@ -65,10 +59,12 @@ public static function parse(string $str) : float {
return bindec($str);
}

// oct
// substr($str, 0, strcspn($str, '89')) cuts the string at the first invalid digit (8 or 9)
// so that only the digits before that are used
return octdec(substr($str, 0, strcspn($str, '89')));
// oct, but only if the string does not contain any of '.eE'.
if (false === strpbrk($str, '.eE')) {
// substr($str, 0, strcspn($str, '89')) cuts the string at the first invalid digit
// (8 or 9) so that only the digits before that are used.
return octdec(substr($str, 0, strcspn($str, '89')));
}
}

// dec
Expand Down
10 changes: 8 additions & 2 deletions test/code/parser/scalar/float.test
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Different float syntaxes
// (all are actually the same number, just in different representations)
18446744073709551615;
0xFFFFFFFFFFFFFFFF;
0xEEEEEEEEEEEEEEEE;
01777777777777777777777;
0177777777777777777777787;
0b1111111111111111111111111111111111111111111111111111111111111111;
Expand Down Expand Up @@ -92,7 +93,7 @@ array(
)
12: Stmt_Expression(
expr: Scalar_DNumber(
value: 1.844674407371E+19
value: 1.7216961135462E+19
)
)
13: Stmt_Expression(
Expand All @@ -105,4 +106,9 @@ array(
value: 1.844674407371E+19
)
)
)
15: Stmt_Expression(
expr: Scalar_DNumber(
value: 1.844674407371E+19
)
)
)

0 comments on commit 7027899

Please sign in to comment.