Skip to content

Commit

Permalink
Merge pull request #82 from dbu/sanity-check-cache-write
Browse files Browse the repository at this point in the history
make sure we only use the cache if write was successful
  • Loading branch information
goetas authored Sep 17, 2019
2 parents 8871a73 + bf2783b commit 8d89581
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Cache/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,20 @@ public function put(ClassMetadata $metadata): void
$path = $this->dir . '/' . strtr($metadata->name, '\\', '-') . '.cache.php';

$tmpFile = tempnam($this->dir, 'metadata-cache');
file_put_contents($tmpFile, '<?php return unserialize(' . var_export(serialize($metadata), true) . ');');
if (false === $tmpFile) {
$this->evict($metadata->name);

return;
}
$data = '<?php return unserialize(' . var_export(serialize($metadata), true) . ');';
$bytesWritten = file_put_contents($tmpFile, $data);
// use strlen and not mb_strlen. if there is utf8 in the code, it also writes more bytes.
if ($bytesWritten !== strlen($data)) {
@unlink($tmpFile);
$this->evict($metadata->name); // also evict the cache to not use an outdated version.

return;
}

// Let's not break filesystems which do not support chmod.
@chmod($tmpFile, 0666 & ~umask());
Expand Down

0 comments on commit 8d89581

Please sign in to comment.