From 0ebf0a4bc6e499ba831d2e316d3128160e5fbc63 Mon Sep 17 00:00:00 2001 From: Stanislav Eismont Date: Tue, 20 Dec 2022 11:09:16 +0300 Subject: [PATCH] add recursion depth limit in to_array_debug() --- kphp_polyfills.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/kphp_polyfills.php b/kphp_polyfills.php index 22338e6..d822b07 100644 --- a/kphp_polyfills.php +++ b/kphp_polyfills.php @@ -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)) { @@ -193,6 +199,7 @@ function to_array_debug($any, $with_class_names = false, $public_members_only = if ($with_class_names) { $result['__class_name'] = get_class($v); } + --$depth; return $result; } if (is_array($v)) { @@ -204,7 +211,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; } /**