[php-src] Issue #7954: eval'ed code doesn't throw TypeError in some cases
| From: | EvgenyLin | Date: | Mon, 17 Jan 2022 07:08:58 +0000 |
| Subject: | [php-src] Issue #7954: eval'ed code doesn't throw TypeError in some cases | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-239046@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/7954
Author: EvgenyLin
### Description
The following code:
```php
<?php
declare(strict_types=1);
class TClass {
public function __construct(String $s) {}
}
$test_values = ['string', null, [1], 1, 3.14, true];
foreach ($test_values as $v) {
try {
$t = new TClass($v);
} catch (TypeError $e) {
// Must be catched for null, Int, Float, Array:
echo 'OK, catched: ' . $e->getMessage() . PHP_EOL;
}
}
echo PHP_EOL . 'Now the same code with eval():' . PHP_EOL;
foreach ($test_values as $v) {
try {
eval('$t = new TClass($v);');
} catch (TypeError $e) {
// Must be catched for null, Int, Float, Array:
echo 'OK, catched: ' . $e->getMessage() . PHP_EOL;
}
}
// BUG: there are NO TypeError for Int, Float, Bool
```
Resulted in this output:
```OK, catched: TClass::__construct(): Argument #1 ($s) must be of type string, null given, called
in /tmp/eval_TypeError_bug.php on line 12
OK, catched: TClass::__construct(): Argument #1 ($s) must be of type string, array given, called in
/tmp/eval_TypeError_bug.php on line 12
OK, catched: TClass::__construct(): Argument #1 ($s) must be of type string, int given, called in
/tmp/eval_TypeError_bug.php on line 12
OK, catched: TClass::__construct(): Argument #1 ($s) must be of type string, float given, called in
/tmp/eval_TypeError_bug.php on line 12
OK, catched: TClass::__construct(): Argument #1 ($s) must be of type string, bool given, called in
/tmp/eval_TypeError_bug.php on line 12
Now the same code with eval():
OK, catched: TClass::__construct(): Argument #1 ($s) must be of type string, null given, called in
/tmp/eval_TypeError_bug.php(24) : eval()'d code on line 1
OK, catched: TClass::__construct(): Argument #1 ($s) must be of type string, array given, called in
/tmp/eval_TypeError_bug.php(24) : eval()'d code on line 1
```
But I expected the same set of TypeError for eval'ed code.
No TypeError were for Int, Float and Bool value.
### PHP Version
PHP 8.1.1 (cli) (built: Dec 31 2021 07:26:20) (NTS)
### Operating System
Linux Dev 5.4.0-94-lowlatency #106-Ubuntu SMP PREEMPT Fri Jan 7 00:37:41 UTC 2022 x86_64 x86_64
x86_64 GNU/Linux