Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7331030

Browse files
[HttpFoundation] use atomic writes in MockFileSessionStorage
1 parent d23b74e commit 7331030

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ public function save()
103103

104104
try {
105105
if ($data) {
106-
file_put_contents($this->getFilePath(), serialize($data));
106+
$path = $this->getFilePath();
107+
$tmp = $path.bin2hex(random_bytes(6));
108+
file_put_contents($tmp, serialize($data));
109+
rename($tmp, $path);
107110
} else {
108111
$this->destroy();
109112
}
@@ -123,8 +126,11 @@ public function save()
123126
*/
124127
private function destroy(): void
125128
{
126-
if (is_file($this->getFilePath())) {
129+
set_error_handler(static function () {});
130+
try {
127131
unlink($this->getFilePath());
132+
} finally {
133+
restore_error_handler();
128134
}
129135
}
130136

0 commit comments

Comments
 (0)