1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-06 05:07:36 +02:00

Fix insane-memory-limit handling in StreamHandler, fixes #1592

This commit is contained in:
Jordi Boggiano
2021-10-01 23:03:11 +02:00
parent 837ef7930d
commit 9d1fed4aff

View File

@@ -26,9 +26,11 @@ use Monolog\Utils;
class StreamHandler extends AbstractProcessingHandler class StreamHandler extends AbstractProcessingHandler
{ {
/** @const int */ /** @const int */
protected const MAX_CHUNK_SIZE = 100 * 1024 * 1024; protected const MAX_CHUNK_SIZE = 2147483647;
/** @const int 10MB */
protected const DEFAULT_CHUNK_SIZE = 10 * 1024 * 1024;
/** @var int */ /** @var int */
protected $streamChunkSize = self::MAX_CHUNK_SIZE; protected $streamChunkSize;
/** @var resource|null */ /** @var resource|null */
protected $stream; protected $stream;
/** @var ?string */ /** @var ?string */
@@ -55,13 +57,15 @@ class StreamHandler extends AbstractProcessingHandler
if (($phpMemoryLimit = Utils::expandIniShorthandBytes(ini_get('memory_limit'))) !== false) { if (($phpMemoryLimit = Utils::expandIniShorthandBytes(ini_get('memory_limit'))) !== false) {
if ($phpMemoryLimit > 0) { if ($phpMemoryLimit > 0) {
// use max 10% of allowed memory for the chunk size // use max 10% of allowed memory for the chunk size, and at least 100KB
$this->streamChunkSize = max((int) ($phpMemoryLimit / 10), 10*1024); $this->streamChunkSize = min(static::MAX_CHUNK_SIZE, max((int) ($phpMemoryLimit / 10), 100 * 1024));
} else {
// memory is unlimited, set to the default 10MB
$this->streamChunkSize = static::DEFAULT_CHUNK_SIZE;
} }
// else memory is unlimited, keep the buffer to the default 100MB
} else { } else {
// no memory limit information, use a conservative 10MB // no memory limit information, set to the default 10MB
$this->streamChunkSize = 10*10*1024; $this->streamChunkSize = static::DEFAULT_CHUNK_SIZE;
} }
if (is_resource($stream)) { if (is_resource($stream)) {