1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-05 12:47:39 +02:00

Don't even try to attempt normalizing iterators or generators in context

Iterators and Generators may not be rewindable, so foreach is not safe
to use on them.

Iterators and especially Generators may trigger irreversible actions on
calling next(), so iterating over all values can potentially cause harm,
e.g. imagine an iterator over a set of HTTP POST requests that are sent
when the next value is requested . The only sufficiently safe thing to
iterate and include here are primitive arrays.
This commit is contained in:
Nils Adermann
2016-11-24 17:28:53 +01:00
parent 3c782662f9
commit 45de570954

View File

@@ -70,17 +70,13 @@ class NormalizerFormatter implements FormatterInterface
return $data;
}
if (is_array($data) || $data instanceof \Traversable) {
if (is_array($data)) {
$normalized = array();
$count = 1;
if ($data instanceof \Generator && !$data->valid()) {
return array('...' => 'Generator is already consumed, aborting');
}
foreach ($data as $key => $value) {
if ($count++ >= 1000) {
$normalized['...'] = 'Over 1000 items ('.($data instanceof \Generator ? 'generator function' : count($data).' total').'), aborting normalization';
$normalized['...'] = 'Over 1000 items ('.count($data).' total), aborting normalization';
break;
}
$normalized[$key] = $this->normalize($value);