8000 bug #38101 [VarExporter] unserialize() might throw an Exception on ph… · symfony/symfony@f8fddf3 · GitHub
[go: up one dir, main page]

Skip to content

Commit f8fddf3

Browse files
bug #38101 [VarExporter] unserialize() might throw an Exception on php 8 (derrabus)
This PR was merged into the 4.4 branch. Discussion ---------- [VarExporter] unserialize() might throw an Exception on php 8 | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #36872 | License | MIT | Doc PR | N/A VarExporter attempts a deserialization in order to check if a given object can be exported in serialized form. On php 8, the `unserialize` call might throw an exception that needs to be caught and converted to the expected `NotInstantiableTypeException`. Commits ------- 65112e1 [VarExporter] unserialize() might throw an Exception on php 8.
2 parents 12b419e + 65112e1 commit f8fddf3

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/Symfony/Component/VarExporter/Exception/NotInstantiableTypeException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
class NotInstantiableTypeException extends \Exception implements ExceptionInterface
1515
{
16-
public function __construct(string $type)
16+
public function __construct(string $type, \Throwable $previous = null)
1717
{
18-
parent::__construct(sprintf('Type "%s" is not instantiable.', $type));
18+
parent::__construct(sprintf('Type "%s" is not instantiable.', $type), 0, $previous);
1919
}
2020
}

src/Symfony/Component/VarExporter/Internal/Registry.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,18 @@ public static function getClassReflector($class, $instantiableWithoutConstructor
8989
$proto = $reflector->implementsInterface('Serializable') && !method_exists($class, '__unserialize') ? 'C:' : 'O:';
9090
if ('C:' === $proto && !$reflector->getMethod('unserialize')->isInternal()) {
9191
$proto = null;
92-
} elseif (false === $proto = @unserialize($proto.\strlen($class).':"'.$class.'":0:{}')) {
93-
throw new NotInstantiableTypeException($class);
92+
} else {
93+
try {
94+
$proto = @unserialize($proto.\strlen($class).':"'.$class.'":0:{}');
95+
} catch (\Exception $e) {
96+
if (__FILE__ !== $e->getFile()) {
97+
throw $e;
98+
}
99+
throw new NotInstantiableTypeException($class, $e);
100+
}
101+
if (false === $proto) {
102+
throw new NotInstantiableTypeException($class);
103+
}
94104
}
95105
}
96106
if (null !== $proto && !$proto instanceof \Throwable && !$proto instanceof \Serializable && !method_exists($class, '__sleep') && (\PHP_VERSION_ID < 70400 || !method_exists($class, '__serialize'))) {

0 commit comments

Comments
 (0)
0