[php-src] Issue #10725: `TypeError` when the array passed to `array_sum` contains array references
| From: | tobyaherbert | Date: | Mon, 27 Feb 2023 22:17:03 +0000 |
| Subject: | [php-src] Issue #10725: `TypeError` when the array passed to `array_sum` contains array references | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-243815@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/10725
Author: tobyaherbert
### Description
Hey there,
I've encountered a problem with the
array_sum function, which only seems to occur
when the passed array contains at least one array reference value.
Here's an example to demonstrate this:
```php
<?php
$value = array(10, array(20), 30);
next($value[1]);
echo array_sum($value);
?>
```
I'm expecting the output of this to be 40, but the following error is thrown
instead:
```
PHP Warning: Uncaught TypeError: Unsupported operand types: int + array in php shell code:1
Stack trace:
#0 php shell code(1): array_sum()
#1 {main}
thrown in php shell code on line 1
```
I've had a look at the PHP source for the array_sum function, and it looks like
array values are skipped, but it doesn't appear to check if the value is a reference to an
array.
I'm not familiar with the internals of PHP, so is someone more familiar with the
array_sum function's implementation able to indicate whether this is intentional?
In the event that this is not intentional, I've come up with a modified version of the
array_sum function which retrieves the type of the underlying value when a reference
value is detected.
Here's a link to that commit in my fork:
https://github.com/php/php-src/commit/f88d67e8b9865c736c931d48b2d5153e8b6d3be3
I don't know if that patch follows the appropriate code syntax or guidelines, as this is the
first time I've ever looked at PHP's source.
Thanks in advance for taking a look
### PHP Version
8.2.3
### Operating System
Ubuntu 22.04