Skip to content

Commit d345421

Browse files
author
Kirill Nesmeyanov
committed
Fix int overflow behaviour
1 parent e9cda57 commit d345421

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/Node/Literal/IntLiteralNode.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,21 @@ public function __construct(
2222

2323
public static function parse(string $value): static
2424
{
25-
[$isNegative, $decimal] = self::split($value);
25+
[$negative, $numeric] = self::split($value);
2626

27-
return new static($isNegative ? -$decimal : $decimal, $value);
27+
if ($negative) {
28+
if ((string) \PHP_INT_MIN === '-' . $numeric) {
29+
return new static(\PHP_INT_MIN, $value);
30+
}
31+
32+
return new static((int) ('-' . $numeric), $value);
33+
}
34+
35+
return new static((int) $numeric, $value);
2836
}
2937

3038
/**
31-
* @return array{bool, int}
39+
* @return array{bool, numeric-string}
3240
*/
3341
private static function split(string $literal): array
3442
{
@@ -40,7 +48,7 @@ private static function split(string $literal): array
4048

4149
// One of: [ 0123, 0o23, 0x00, 0b01 ]
4250
if ($literal[0] === '0' && isset($literal[1])) {
43-
return [$isNegative, (int) (match ($literal[1]) {
51+
return [$isNegative, (match ($literal[1]) {
4452
// hexadecimal
4553
'x', 'X' => \hexdec(\substr($literal, 2)),
4654
// binary
@@ -52,7 +60,7 @@ private static function split(string $literal): array
5260
})];
5361
}
5462

55-
return [$isNegative, (int) $literal];
63+
return [$isNegative, $literal];
5664
}
5765

5866
public function getValue(): int

0 commit comments

Comments
 (0)