bug #20671 [Config] ConfigCache::isFresh() should return false when unserialize() fails (nicolas-grekas)

This PR was merged into the 2.7 branch.

Discussion
----------

[Config] ConfigCache::isFresh() should return false when unserialize() fails

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20654
| License       | MIT
| Doc PR        | -

Removes some `Warning: Class __PHP_Incomplete_Class has no unserializer` failures when clearing the cache.

Commits
-------

609245e [Config] ConfigCache::isFresh() should return false on __PHP_Incomplete_Class
This commit is contained in:
Nicolas Grekas 2016-11-29 11:51:46 +01:00
commit c360a222ef
2 changed files with 35 additions and 1 deletions

View File

@ -85,8 +85,33 @@ class ConfigCache implements ConfigCacheInterface
return false;
}
$e = null;
$meta = false;
$time = filemtime($this->file);
$meta = unserialize(file_get_contents($metadata));
$signalingException = new \UnexpectedValueException();
$prevUnserializeHandler = ini_set('unserialize_callback_func', '');
$prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context) use (&$prevErrorHandler, $signalingException) {
if (E_WARNING === $type && 'Class __PHP_Incomplete_Class has no unserializer' === $msg) {
throw $signalingException;
}
return $prevErrorHandler ? $prevErrorHandler($type, $msg, $file, $line, $context) : false;
});
try {
$meta = unserialize(file_get_contents($metadata));
} catch (\Error $e) {
} catch (\Exception $e) {
}
restore_error_handler();
ini_set('unserialize_callback_func', $prevUnserializeHandler);
if (null !== $e && $e !== $signalingException) {
throw $e;
}
if (false === $meta) {
return false;
}
foreach ($meta as $resource) {
if (!$resource->isFresh($time)) {
return false;

View File

@ -93,6 +93,15 @@ class ConfigCacheTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($cache->isFresh());
}
public function testCacheIsNotFreshWhenUnserializeFails()
{
file_put_contents($this->metaFile, str_replace('FileResource', 'ClassNotHere', file_get_contents($this->metaFile)));
$cache = new ConfigCache($this->cacheFile, true);
$this->assertFalse($cache->isFresh());
}
public function testWriteDumpsFile()
{
unlink($this->cacheFile);