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:
Nicolas Grekas 2020-09-08 09:04:33 +02:00
commit f8fddf3917
2 changed files with 14 additions and 4 deletions

View File

@ -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);
}
}

View File

@ -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'))) {