Skip to content

Promote warnings to errors in array_merge(_recursive)() and array_replace() #4586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -3572,7 +3572,7 @@ PHPAPI int php_array_merge_recursive(HashTable *dest, HashTable *src) /* {{{ */
ZVAL_DEREF(dest_zval);
thash = Z_TYPE_P(dest_zval) == IS_ARRAY ? Z_ARRVAL_P(dest_zval) : NULL;
if ((thash && GC_IS_RECURSIVE(thash)) || (src_entry == dest_entry && Z_ISREF_P(dest_entry) && (Z_REFCOUNT_P(dest_entry) % 2))) {
php_error_docref(NULL, E_WARNING, "recursion detected");
zend_throw_error(NULL, "Recursion detected");
return 0;
}

Expand Down Expand Up @@ -3693,7 +3693,7 @@ PHPAPI int php_array_replace_recursive(HashTable *dest, HashTable *src) /* {{{ *
if (Z_IS_RECURSIVE_P(dest_zval) ||
Z_IS_RECURSIVE_P(src_zval) ||
(Z_ISREF_P(src_entry) && Z_ISREF_P(dest_entry) && Z_REF_P(src_entry) == Z_REF_P(dest_entry) && (Z_REFCOUNT_P(dest_entry) % 2))) {
php_error_docref(NULL, E_WARNING, "recursion detected");
zend_throw_error(NULL, "Recursion detected");
return 0;
}

Expand Down
24 changes: 8 additions & 16 deletions ext/standard/tests/array/array_replace.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ $data = array_replace_recursive($array1, $array2);
var_dump($data);

echo " -- Testing array_replace_recursive() w/ endless recusrsion --\n";
$data = array_replace_recursive($array3, $array4);
try {
$data = array_replace_recursive($array3, $array4);
var_dump($data);
} catch (\Error $e) {
echo $e->getMessage() . "\n";
}

var_dump($data);
?>
--EXPECTF--
--EXPECT--
-- Testing array_replace() --
array(5) {
[0]=>
Expand Down Expand Up @@ -107,16 +111,4 @@ array(5) {
}
}
-- Testing array_replace_recursive() w/ endless recusrsion --

Warning: array_replace_recursive(): recursion detected in %s on line %d
array(1) {
[0]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(0) {
}
}
}
}
Recursion detected
10 changes: 7 additions & 3 deletions ext/standard/tests/array/bug42177.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ unset( $a1, $a2 );
$a1 = array();
$a2 = array( 'key1' => &$a1 );
$a1 = array_merge_recursive( $a1, $a2 );
$a1 = array_merge_recursive( $a1, $a2 );
try {
$a1 = array_merge_recursive( $a1, $a2 );
} catch (\Error $e) {
echo $e->getMessage() . " on line " . $e->getLine() . "\n";
}
unset( $a1, $a2 );

$x = 'foo';
Expand All @@ -30,5 +34,5 @@ $a1 = array_merge_recursive( $a1, $a2 );
unset( $a1, $a2 );

?>
--EXPECTF--
Warning: array_merge_recursive(): recursion detected in %s on line 18
--EXPECT--
Recursion detected on line 19
17 changes: 12 additions & 5 deletions ext/standard/tests/array/bug43495.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ $a["key1"]["key2"]["key3"]=&$a;
$b=array("key1"=>array("key2"=>array()));
$b["key1"]["key2"]["key3"]=&$b;

array_merge_recursive($a,$b);

try {
array_merge_recursive($a,$b);
} catch (\Error $e) {
echo $e->getMessage() . "\n";
}

/* Break recursion */
$a["key1"]["key2"]["key3"] = null;
$b["key1"]["key2"]["key3"] = null;

echo "Done.\n";
?>
--EXPECTF--
Warning: array_merge_recursive(): recursion detected in %sbug43495.php on line %d
Done.

DONE
--EXPECT--
Recursion detected

DONE
10 changes: 7 additions & 3 deletions ext/standard/tests/array/bug65251.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ Bug #65251: array_merge_recursive() recursion detection broken
--FILE--
<?php

array_merge_recursive($GLOBALS, $GLOBALS)
try {
array_merge_recursive($GLOBALS, $GLOBALS);
} catch (\Error $e) {
echo $e->getMessage() . "\n";
}

?>
--EXPECTF--
Warning: array_merge_recursive(): recursion detected in %s on line %d
--EXPECT--
Recursion detected