8000 bug #39816 [HttpFoundation] use atomic writes in MockFileSessionStora… · symfony/symfony@9c6381c · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c6381c

Browse files
bug #39816 [HttpFoundation] use atomic writes in MockFileSessionStorage (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpFoundation] use atomic writes in MockFileSessionStorage | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #39167 | License | MIT | Doc PR | - Instead of #39808 Commits ------- 5290e97 [HttpFoundation] use atomic writes in MockFileSessionStorage
2 parents f021d6f + 5290e97 commit 9c6381c

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

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

Lines changed: 16 additions & 4 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

@@ -141,8 +147,14 @@ private function getFilePath(): string
141147
*/
142148
private function read(): void
143149
{
144-
$filePath = $this->getFilePath();
145-
$this->data = is_readable($filePath) && is_file($filePath) ? unserialize(file_get_contents($filePath)) : [];
150+
set_error_handler(static function () {});
151+
try {
152+
$data = file_get_contents($this->getFilePath());
153+
} finally {
154+
restore_error_handler();
155+
}
156+
157+
$this->data = $data ? unserialize($data) : [];
146158

147159
$this->loadSession();
148160
}

0 commit comments

Comments
 (0)
0