8000 [Cache] Throw ValueError in debug mode when serialization fails by nicolas-grekas · Pull Request #43148 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

8000 [Cache] Throw ValueError in debug mode when serialization fails #43148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@
->set('cache.default_marshaller', DefaultMarshaller::class)
->args([
null, // use igbinary_serialize() when available
'%kernel.debug%',
])

->set('cache.early_expiration_handler', EarlyExpirationHandler::class)
Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Component/Cache/Marshaller/DefaultMarshaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@
class DefaultMarshaller implements MarshallerInterface
{
private $useIgbinarySerialize = true;
private $throwOnSerializationFailure;

public function __construct(bool $useIgbinarySerialize = null)
public function __construct(bool $useIgbinarySerialize = null, bool $throwOnSerializationFailure = false)
{
if (null === $useIgbinarySerialize) {
$useIgbinarySerialize = \extension_loaded('igbinary') && (\PHP_VERSION_ID < 70400 || version_compare('3.1.6', phpversion('igbinary'), '<='));
} elseif ($useIgbinarySerialize && (!\extension_loaded('igbinary') || (\PHP_VERSION_ID >= 70400 && version_compare('3.1.6', phpversion('igbinary'), '>')))) {
throw new CacheException(\extension_loaded('igbinary') && \PHP_VERSION_ID >= 70400 ? 'Please upgrade the "igbinary" PHP extension to v3.1.6 or higher.' : 'The "igbinary" PHP extension is not loaded.');
}
$this->useIgbinarySerialize = $useIgbinarySerialize;
$this->throwOnSerializationFailure = $throwOnSerializationFailure;
}

/**
Expand All @@ -47,6 +49,9 @@ public function marshall(array $values, ?array &$failed): array
$serialized[$id] = serialize($value);
}
} catch (\Exception $e) {
if ($this->throwOnSerializationFailure) {
throw new \ValueError($e->getMessage(), 0, $e);
}
$failed[] = $id;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,15 @@ public function testIgbinaryUnserializeInvalid()
restore_error_handler();
}
}

public function testSerializeDebug()
{
$marshaller = new DefaultMarshaller(false, true);
$values = [
'a' => function () {},
];

$this->expectException(\ValueError::class);
$marshaller->marshall($values, $failed);
}
}
0