Skip to content

Commit

Permalink
add recursion depth limit in to_array_debug()
Browse files Browse the repository at this point in the history
  • Loading branch information
drdzyk committed Dec 20, 2022
1 parent 571cc4d commit 57ddfee
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions kphp_polyfills.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,14 @@ function to_array_debug($any, $with_class_names = false, $public_members_only =
return $key;
};

$toArray = function($v) use (&$toArray, &$demangleField, &$with_class_names, $public_members_only, $isPrivateField) {
$depth = 0;
$recursion_depth_exceeded = false;
$toArray = function($v) use (&$toArray, &$demangleField, &$with_class_names, $public_members_only, $isPrivateField, &$depth, &$recursion_depth_exceeded) {
if (is_object($v)) {
if ($depth++ > 64) {
$recursion_depth_exceeded = true;
return [];
}
$result = [];
foreach ((array)$v as $field => $value) {
if ($public_members_only && $isPrivateField($field)) {
Expand All @@ -204,7 +210,8 @@ function to_array_debug($any, $with_class_names = false, $public_members_only =
if ($any === null) {
return [];
}
return $toArray($any);
$result = $toArray($any);
return $recursion_depth_exceeded ? [] : $result;
}

/**
Expand Down

0 comments on commit 57ddfee

Please sign in to comment.