Skip to content

Commit 0ebf0a4

Browse files
committed
add recursion depth limit in to_array_debug()
1 parent 571cc4d commit 0ebf0a4

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

kphp_polyfills.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,14 @@ function to_array_debug($any, $with_class_names = false, $public_members_only =
181181
return $key;
182182
};
183183

184-
$toArray = function($v) use (&$toArray, &$demangleField, &$with_class_names, $public_members_only, $isPrivateField) {
184+
$depth = 0;
185+
$recursion_depth_exceeded = false;
186+
$toArray = function($v) use (&$toArray, &$demangleField, &$with_class_names, $public_members_only, $isPrivateField, &$depth, &$recursion_depth_exceeded) {
185187
if (is_object($v)) {
188+
if ($depth++ > 64) {
189+
$recursion_depth_exceeded = true;
190+
return [];
191+
}
186192
$result = [];
187193
foreach ((array)$v as $field => $value) {
188194
if ($public_members_only && $isPrivateField($field)) {
@@ -193,6 +199,7 @@ function to_array_debug($any, $with_class_names = false, $public_members_only =
193199
if ($with_class_names) {
194200
$result['__class_name'] = get_class($v);
195201
}
202+
--$depth;
196203
return $result;
197204
}
198205
if (is_array($v)) {
@@ -204,7 +211,8 @@ function to_array_debug($any, $with_class_names = false, $public_members_only =
204211
if ($any === null) {
205212
return [];
206213
}
207-
return $toArray($any);
214+
$result = $toArray($any);
215+
return $recursion_depth_exceeded ? [] : $result;
208216
}
209217

210218
/**

0 commit comments

Comments
 (0)