1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-06 13:16:39 +02:00

Merge pull request #808 from naderman/fix/normalizer-recusion

Normalization of arrays containing self references
This commit is contained in:
Jordi Boggiano
2016-07-02 15:34:40 +02:00
committed by GitHub
4 changed files with 23 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 = []; $normalized = [];
@@ -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

@@ -56,8 +56,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)) {
@@ -80,7 +84,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 \DateTimeInterface) { if (is_object($data) && !$data instanceof \DateTimeInterface) {
return $data; return $data;
} }
return parent::normalize($data); return parent::normalize($data, $depth);
} }
} }

View File

@@ -166,6 +166,15 @@ class NormalizerFormatterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(@json_encode([$foo, $bar]), $res); $this->assertEquals(@json_encode([$foo, $bar]), $res);
} }
public function testCanNormalizeReferences()
{
$formatter = new NormalizerFormatter();
$x = ['foo' => 'bar'];
$y = ['x' => &$x];
$x['y'] = &$y;
$formatter->format($y);
}
public function testIgnoresInvalidTypes() public function testIgnoresInvalidTypes()
{ {
// set up the recursion // set up the recursion