8000 [Cache] fix possible collision when writing tmp file in filesystem ad… · symfony/symfony@340d15e · GitHub
[go: up one dir, main page]

Skip to content

Commit 340d15e

Browse files
[Cache] fix possible collision when writing tmp file in filesystem adapter
1 parent c79e61f commit 340d15e

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/Symfony/Component/Cache/Traits/FilesystemCommonTrait.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,20 @@ private function write(string $file, string $data, int $expiresAt = null)
9393
set_error_handler(__CLASS__.'::throwError');
9494
try {
9595
if (null === $this->tmp) {
96-
$this->tmp = $this->directory.uniqid('', true);
96+
$this->tmp = $this->directory.bin2hex(random_bytes(6));
9797
}
98-
file_put_contents($this->tmp, $data);
98+
try {
99+
$h = fopen($this->tmp, 'x');
100+
} catch (\ErrorException $e) {
101+
if (false === strpos($e->getMessage(), 'File exists')) {
102+
throw $e;
103+
}
104+
105+
$this->tmp = $this->directory.bin2hex(random_bytes(6));
106+
$h = fopen($this->tmp, 'x');
107+
}
108+
fwrite($h, $data);
109+
fclose($h);
99110

100111
if (null !== $expiresAt) {
101112
touch($this->tmp, $expiresAt);

0 commit comments

Comments
 (0)
0