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
-------
65112e11bc
[VarExporter] unserialize() might throw an Exception on php 8.
This commit is contained in:
commit
f8fddf3917
@ -13,8 +13,8 @@ namespace Symfony\Component\VarExporter\Exception;
|
||||
|
||||
class NotInstantiableTypeException extends \Exception implements ExceptionInterface
|
||||
{
|
||||
public function __construct(string $type)
|
||||
public function __construct(string $type, \Throwable $previous = null)
|
||||
{
|
||||
parent::__construct(sprintf('Type "%s" is not instantiable.', $type));
|
||||
parent::__construct(sprintf('Type "%s" is not instantiable.', $type), 0, $previous);
|
||||
}
|
||||
}
|
||||
|
@ -89,8 +89,18 @@ class Registry
|
||||
$proto = $reflector->implementsInterface('Serializable') && !method_exists($class, '__unserialize') ? 'C:' : 'O:';
|
||||
if ('C:' === $proto && !$reflector->getMethod('unserialize')->isInternal()) {
|
||||
$proto = null;
|
||||
} elseif (false === $proto = @unserialize($proto.\strlen($class).':"'.$class.'":0:{}')) {
|
||||
throw new NotInstantiableTypeException($class);
|
||||
} else {
|
||||
try {
|
||||
$proto = @unserialize($proto.\strlen($class).':"'.$class.'":0:{}');
|
||||
} catch (\Exception $e) {
|
||||
if (__FILE__ !== $e->getFile()) {
|
||||
throw $e;
|
||||
}
|
||||
throw new NotInstantiableTypeException($class, $e);
|
||||
}
|
||||
if (false === $proto) {
|
||||
throw new NotInstantiableTypeException($class);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (null !== $proto && !$proto instanceof \Throwable && !$proto instanceof \Serializable && !method_exists($class, '__sleep') && (\PHP_VERSION_ID < 70400 || !method_exists($class, '__serialize'))) {
|
||||
|
Reference in New Issue
Block a user