Skip to content
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

always use lock when writing/reading cache data to/from file #8372

Merged
merged 1 commit into from Aug 11, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php
Expand Up @@ -3,13 +3,13 @@
namespace Psalm\Internal\Provider;

use Psalm\Config;
use Psalm\Internal\Provider\Providers;
use Psalm\Storage\ClassLikeStorage;
use UnexpectedValueException;

use function array_merge;
use function dirname;
use function file_exists;
use function file_get_contents;
use function file_put_contents;
use function filemtime;
use function get_class;
Expand All @@ -25,6 +25,7 @@
use function unserialize;

use const DIRECTORY_SEPARATOR;
use const LOCK_EX;
use const PHP_VERSION_ID;

/**
Expand Down Expand Up @@ -86,9 +87,9 @@ public function writeToCache(ClassLikeStorage $storage, string $file_path, strin

$cache_location = $this->getCacheLocationForClass($fq_classlike_name_lc, $file_path, true);
if ($this->config->use_igbinary) {
file_put_contents($cache_location, igbinary_serialize($storage));
file_put_contents($cache_location, igbinary_serialize($storage), LOCK_EX);
} else {
file_put_contents($cache_location, serialize($storage));
file_put_contents($cache_location, serialize($storage), LOCK_EX);
}
}

Expand Down Expand Up @@ -132,7 +133,7 @@ private function loadFromCache(string $fq_classlike_name_lc, ?string $file_path)

if (file_exists($cache_location)) {
if ($this->config->use_igbinary) {
$storage = igbinary_unserialize((string)file_get_contents($cache_location));
$storage = igbinary_unserialize(Providers::safeFileGetContents($cache_location));

if ($storage instanceof ClassLikeStorage) {
return $storage;
Expand All @@ -141,7 +142,7 @@ private function loadFromCache(string $fq_classlike_name_lc, ?string $file_path)
return null;
}

$storage = unserialize((string)file_get_contents($cache_location));
$storage = unserialize(Providers::safeFileGetContents($cache_location));

if ($storage instanceof ClassLikeStorage) {
return $storage;
Expand Down