Skip to content

Commit 36e4ec6

Browse files
[Config] ConfigCache::isFresh() should return false when unserialize() fails
1 parent 777cdfc commit 36e4ec6

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/Symfony/Component/Config/ConfigCache.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,26 @@ public function isFresh()
8686
}
8787

8888
$time = filemtime($this->file);
89-
$meta = unserialize(file_get_contents($metadata));
89+
try {
90+
$unserializeCallbackHandler = ini_set('unserialize_callback_func', '');
91+
$signalingException = new \UnexpectedValueException();
92+
set_error_handler(function () use ($signalingException) { throw $signalingException; });
93+
94+
$e = null;
95+
$meta = false;
96+
$meta = unserialize(file_get_contents($metadata));
97+
} catch (\Error $e) {
98+
} catch (\Exception $e) {
99+
}
100+
restore_error_handler();
101+
ini_set('unserialize_callback_func', $unserializeCallbackHandler);
102+
if (null !== $e && $e !== $signalingException) {
103+
throw $e;
104+
}
105+
if (false === $meta) {
106+
return false;
107+
}
108+
90109
foreach ($meta as $resource) {
91110
if (!$resource->isFresh($time)) {
92111
return false;

src/Symfony/Component/Config/Tests/ConfigCacheTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ public function testCacheIsNotFreshIfOneOfTheResourcesIsNotFresh()
9393
$this->assertFalse($cache->isFresh());
9494
}
9595

96+
public function testCacheIsNotFreshWhenUnserializeFails()
97+
{
98+
file_put_contents($this->metaFile, substr(file_get_contents($this->metaFile), 2));
99+
100+
$cache = new ConfigCache($this->cacheFile, true);
101+
102+
$this->assertFalse($cache->isFresh());
103+
}
104+
96105
public function testWriteDumpsFile()
97106
{
98107
unlink($this->cacheFile);

0 commit comments

Comments
 (0)