php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45700 array_reduce only accepts integer as $initial
Submitted: 2008-08-03 14:18 UTC Modified: 2008-08-04 22:00 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: j dot jeising at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.6 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: j dot jeising at gmail dot com
New email:
PHP Version: OS:

 

 [2008-08-03 14:18 UTC] j dot jeising at gmail dot com
Description:
------------
array_reduce only accepts integer as the $initial value. All other values are converted to long. This problem was already discussed in #42566, but the only answer is thats because of a fix to #29954. The fix for #29954 was the following change:

if (ZEND_NUM_ARGS() > 2) {
	convert_to_long_ex(initial);
	result = *initial;
}

http://cvs.php.net/viewcvs.cgi/php-src/ext/standard/array.c?r1=1.283&r2=1.284

That's not a proper fix to the problem. You can't solve a segfault with a type conversion - some functionality is missing here.

Reproduce code:
---------------
$array = array ( array ( 1 ), array ( 2 ) );
	
print_r ( array_reduce ( $array, 'array_merge', array ( ) );

Expected result:
----------------
Array ( [0] => 1, [1] => 2 );

Actual result:
--------------
Warning: array_merge() [function.array-merge]: Argument #1 is not an array in test.php on line 3

=> initial value is 1.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-08-04 21:26 UTC] jani@php.net
Using PHP 5.3:

Warning: array_reduce() expects parameter 3 to be long, array given in /usr/src/build/php_5_3CVS_tst/t.php on line 10

Current behaviour in 5.2 has existed long enough not to be broken in PHP 5.2 branch.
 [2008-08-04 22:00 UTC] j dot jeising at gmail dot com
Year, cool way to solve problems - "Exists long enough to not be a bug(, for all unbelievers add an error to the next version." But the problem still exists - why should I only use the function with long values? Why not for arrays or other types? I can tell you: the way in this case was the following:

1. (4.0.5) Add PHP function array_reduce, need something to implement some mathematical operations

2. (5.0.1) Some changed behaviours in PHP 5 cause a segfault if an array is passed, let's convert it to long, according to the orignal target. Oh and change the docs, it takes a int instead of mixed as last parameter.

3. (5.3) Let's clean everything up, we throw errors in other functions with types, let's do it here, too.

=> The problem is, that the original segfault is not realy fixed. Perhaps it's already solved in PHP 5.3 (so you only have to remove the conversion code), perhaps not. But this is not a bogus, it's not the goal of the function, it's simply a bad fixed bug and it's story. So please change status back to Open (/Feature Request) and fix it!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 14:01:31 2024 UTC