1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-22 17:16:18 +02:00

Introduce a depth parameter for normalization to limit stack nesting

This commit is contained in:
Nils Adermann
2016-06-22 14:36:16 +02:00
parent c582a6e791
commit 50232e7bb4
3 changed files with 14 additions and 6 deletions

View File

@@ -135,8 +135,12 @@ class JsonFormatter extends NormalizerFormatter
* *
* @return mixed * @return mixed
*/ */
protected function normalize($data) protected function normalize($data, $depth = 0)
{ {
if ($depth > 9) {
return 'Over 9 levels deep, aborting normalization';
}
if (is_array($data) || $data instanceof \Traversable) { if (is_array($data) || $data instanceof \Traversable) {
$normalized = array(); $normalized = array();
@@ -146,7 +150,7 @@ class JsonFormatter extends NormalizerFormatter
$normalized['...'] = 'Over 1000 items, aborting normalization'; $normalized['...'] = 'Over 1000 items, aborting normalization';
break; break;
} }
$normalized[$key] = $this->normalize($value); $normalized[$key] = $this->normalize($value, $depth+1);
} }
return $normalized; return $normalized;

View File

@@ -55,8 +55,12 @@ class NormalizerFormatter implements FormatterInterface
return $records; return $records;
} }
protected function normalize($data) protected function normalize($data, $depth = 0)
{ {
if ($depth > 9) {
return 'Over 9 levels deep, aborting normalization';
}
if (null === $data || is_scalar($data)) { if (null === $data || is_scalar($data)) {
if (is_float($data)) { if (is_float($data)) {
if (is_infinite($data)) { if (is_infinite($data)) {
@@ -79,7 +83,7 @@ class NormalizerFormatter implements FormatterInterface
$normalized['...'] = 'Over 1000 items, aborting normalization'; $normalized['...'] = 'Over 1000 items, aborting normalization';
break; break;
} }
$normalized[$key] = $this->normalize($value); $normalized[$key] = $this->normalize($value, $depth+1);
} }
return $normalized; return $normalized;

View File

@@ -102,12 +102,12 @@ class WildfireFormatter extends NormalizerFormatter
throw new \BadMethodCallException('Batch formatting does not make sense for the WildfireFormatter'); throw new \BadMethodCallException('Batch formatting does not make sense for the WildfireFormatter');
} }
protected function normalize($data) protected function normalize($data, $depth = 0)
{ {
if (is_object($data) && !$data instanceof \DateTime) { if (is_object($data) && !$data instanceof \DateTime) {
return $data; return $data;
} }
return parent::normalize($data); return parent::normalize($data, $depth);
} }
} }