parent
0821f62f20
commit
a4dee96fcf
6 changed files with 244 additions and 7 deletions
@ -0,0 +1,58 @@ |
|||||||
|
--TEST-- |
||||||
|
Runtime integer overflow is checked at an int return boundary |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
declare(strict_types=1); |
||||||
|
|
||||||
|
final class OverflowProperties |
||||||
|
{ |
||||||
|
public int $left = 0; |
||||||
|
public int $right = 0; |
||||||
|
|
||||||
|
public function sum(): int |
||||||
|
{ |
||||||
|
return $this->left + $this->right; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
function addInts(int $left, int $right): int |
||||||
|
{ |
||||||
|
return $left + $right; |
||||||
|
} |
||||||
|
|
||||||
|
function subtractInts(int $left, int $right): int |
||||||
|
{ |
||||||
|
return $left - $right; |
||||||
|
} |
||||||
|
|
||||||
|
function multiplyInts(int $left, int $right): int |
||||||
|
{ |
||||||
|
return $left * $right; |
||||||
|
} |
||||||
|
|
||||||
|
function main(): void |
||||||
|
{ |
||||||
|
foreach ([ |
||||||
|
static fn (): int => addInts(PHP_INT_MAX, 1), |
||||||
|
static fn (): int => subtractInts(PHP_INT_MIN, 1), |
||||||
|
static fn (): int => multiplyInts(PHP_INT_MAX, 2), |
||||||
|
static function (): int { |
||||||
|
$value = new OverflowProperties(); |
||||||
|
$value->left = PHP_INT_MAX; |
||||||
|
$value->right = 1; |
||||||
|
return $value->sum(); |
||||||
|
}, |
||||||
|
] as $callback) { |
||||||
|
try { |
||||||
|
var_dump($callback()); |
||||||
|
} catch (TypeError $error) { |
||||||
|
echo $error->getMessage(), "\n"; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECTF-- |
||||||
|
addInts(): Return value must be of type int, float returned |
||||||
|
subtractInts(): Return value must be of type int, float returned |
||||||
|
multiplyInts(): Return value must be of type int, float returned |
||||||
|
OverflowProperties::sum(): Return value must be of type int, float returned |
||||||
@ -0,0 +1,48 @@ |
|||||||
|
--TEST-- |
||||||
|
Final int property addition uses a detached value accumulator |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
declare(strict_types=1); |
||||||
|
|
||||||
|
final class AddChain |
||||||
|
{ |
||||||
|
public int $first = 1; |
||||||
|
public int $second = 2; |
||||||
|
public int $third = 3; |
||||||
|
public int $fourth = 4; |
||||||
|
public int $fifth = 5; |
||||||
|
|
||||||
|
public function sum(): int |
||||||
|
{ |
||||||
|
return $this->first + $this->second + $this->third + $this->fourth + $this->fifth; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
function main(): void |
||||||
|
{ |
||||||
|
$value = new AddChain(); |
||||||
|
$first =& $value->first; |
||||||
|
|
||||||
|
var_dump($value->sum()); |
||||||
|
var_dump($value->first, $first); |
||||||
|
|
||||||
|
$value->first = PHP_INT_MAX; |
||||||
|
$value->second = 1; |
||||||
|
$value->third = 0; |
||||||
|
$value->fourth = 0; |
||||||
|
$value->fifth = 0; |
||||||
|
try { |
||||||
|
var_dump($value->sum()); |
||||||
|
} catch (TypeError $error) { |
||||||
|
echo $error->getMessage(), "\n"; |
||||||
|
} |
||||||
|
var_dump($value->first, $first); |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECTF-- |
||||||
|
int(15) |
||||||
|
int(1) |
||||||
|
int(1) |
||||||
|
AddChain::sum(): Return value must be of type int, float returned |
||||||
|
int(9223372036854775807) |
||||||
|
int(9223372036854775807) |
||||||
Loading…
Reference in new issue