Skip to content

Commit 0d993d8

Browse files
deviantintegralSeldaek
authored andcommitted
Normalization of arrays containing self references (#1050)
Backport normalization fix from master to 1.x
1 parent 6e1793e commit 0d993d8

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

src/Monolog/Formatter/JsonFormatter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,12 @@ protected function formatBatchNewlines(array $records)
138138
*
139139
* @return mixed
140140
*/
141-
protected function normalize($data)
141+
protected function normalize($data, $depth = 0)
142142
{
143+
if ($depth > 9) {
144+
return 'Over 9 levels deep, aborting normalization';
145+
}
146+
143147
if (is_array($data) || $data instanceof \Traversable) {
144148
$normalized = array();
145149

@@ -150,7 +154,7 @@ protected function normalize($data)
150154
break;
151155
}
152156

153-
$normalized[$key] = $this->normalize($value);
157+
$normalized[$key] = $this->normalize($value, $depth+1);
154158
}
155159

156160
return $normalized;

src/Monolog/Formatter/NormalizerFormatter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ public function formatBatch(array $records)
5555
return $records;
5656
}
5757

58-
protected function normalize($data)
58+
protected function normalize($data, $depth = 0)
5959
{
60+
if ($depth > 9) {
61+
return 'Over 9 levels deep, aborting normalization';
62+
}
63+
6064
if (null === $data || is_scalar($data)) {
6165
if (is_float($data)) {
6266
if (is_infinite($data)) {
@@ -80,7 +84,7 @@ protected function normalize($data)
8084
break;
8185
}
8286

83-
$normalized[$key] = $this->normalize($value);
87+
$normalized[$key] = $this->normalize($value, $depth+1);
8488
}
8589

8690
return $normalized;

src/Monolog/Formatter/WildfireFormatter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ public function formatBatch(array $records)
102102
throw new \BadMethodCallException('Batch formatting does not make sense for the WildfireFormatter');
103103
}
104104

105-
protected function normalize($data)
105+
protected function normalize($data, $depth = 0)
106106
{
107107
if (is_object($data) && !$data instanceof \DateTime) {
108108
return $data;
109109
}
110110

111-
return parent::normalize($data);
111+
return parent::normalize($data, $depth);
112112
}
113113
}

tests/Monolog/Formatter/NormalizerFormatterTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,15 @@ public function testIgnoresRecursiveObjectReferences()
193193
$this->assertEquals(@json_encode(array($foo, $bar)), $res);
194194
}
195195

196+
public function testCanNormalizeReferences()
197+
{
198+
$formatter = new NormalizerFormatter();
199+
$x = array('foo' => 'bar');
200+
$y = array('x' => &$x);
201+
$x['y'] = &$y;
202+
$formatter->format($y);
203+
}
204+
196205
public function testIgnoresInvalidTypes()
197206
{
198207
// set up the recursion

0 commit comments

Comments
 (0)