Skip to content

Commit

Permalink
Fix fatal error in NormalizeFormatter: method_exists(): The script tr…
Browse files Browse the repository at this point in the history
…ied to execute a method or access a property of an incomplete object.

Closes #1833
Fixes #1834
  • Loading branch information
koekaverna authored and Seldaek committed Oct 27, 2023
1 parent 70e1c2f commit ed80d53
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Monolog/Formatter/NormalizerFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ protected function normalize($data, int $depth = 0)
if ($data instanceof \JsonSerializable) {
/** @var null|scalar|array<array|scalar|null> $value */
$value = $data->jsonSerialize();
} elseif (\get_class($data) === '__PHP_Incomplete_Class') {
$accessor = new \ArrayObject($data);
$value = $accessor['__PHP_Incomplete_Class_Name'];
} elseif (method_exists($data, '__toString')) {
/** @var string $value */
$value = $data->__toString();
Expand Down
14 changes: 14 additions & 0 deletions tests/Monolog/Formatter/NormalizerFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,20 @@ public function testExceptionTraceDoesNotLeakCallUserFuncArgs()
);
}

public function testCanNormalizeIncompleteObject(): void
{
$serialized = "O:17:\"Monolog\TestClass\":1:{s:23:\"\x00Monolog\TestClass\x00name\";s:4:\"test\";}";
$object = unserialize($serialized);

$formatter = new NormalizerFormatter();
$record = ['context' => ['object' => $object]];
$result = $formatter->format($record);

$this->assertEquals([
'__PHP_Incomplete_Class' => 'Monolog\\TestClass',
], $result['context']['object']);
}

private function throwHelper($arg)
{
throw new \RuntimeException('Thrown');
Expand Down

0 comments on commit ed80d53

Please sign in to comment.