Skip to content

Commit 318c16d

Browse files
committed
[4.2.x] Fixed #36298 -- Truncated the overwritten file content in file_move_safe().
Regression in 58cd490. Thanks Baptiste Mispelon for the report. Backport of 8ad3e80 from main.
1 parent 506cf74 commit 318c16d

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

django/core/files/move.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def file_move_safe(
6767
| os.O_CREAT
6868
| getattr(os, "O_BINARY", 0)
6969
| (os.O_EXCL if not allow_overwrite else 0)
70+
| os.O_TRUNC
7071
),
7172
)
7273
try:

docs/releases/4.2.21.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
===========================
2+
Django 4.2.21 release notes
3+
===========================
4+
5+
*Expected May 7, 2025*
6+
7+
Django 4.2.21 fixes a data loss bug in 4.2.20.
8+
9+
Bugfixes
10+
========
11+
12+
* Fixed a data corruption possibility in ``file_move_safe()`` when
13+
``allow_overwrite=True``, where leftover content from a previously larger
14+
file could remain after overwriting with a smaller one due to lack of
15+
truncation (:ticket:`36298`).

docs/releases/index.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ versions of the documentation contain the release notes for any later releases.
2626
.. toctree::
2727
:maxdepth: 1
2828

29+
4.2.21
2930
4.2.20
3031
4.2.19
3132
4.2.18

tests/files/tests.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,27 @@ def test_file_move_permissionerror(self):
475475
os.close(handle_b)
476476
os.close(handle_c)
477477

478+
def test_file_move_ensure_truncation(self):
479+
with tempfile.NamedTemporaryFile(delete=False) as src:
480+
src.write(b"content")
481+
src_name = src.name
482+
self.addCleanup(
483+
lambda: os.remove(src_name) if os.path.exists(src_name) else None
484+
)
485+
486+
with tempfile.NamedTemporaryFile(delete=False) as dest:
487+
dest.write(b"This is a longer content.")
488+
dest_name = dest.name
489+
self.addCleanup(os.remove, dest_name)
490+
491+
with mock.patch("django.core.files.move.os.rename", side_effect=OSError()):
492+
file_move_safe(src_name, dest_name, allow_overwrite=True)
493+
494+
with open(dest_name, "rb") as f:
495+
content = f.read()
496+
497+
self.assertEqual(content, b"content")
498+
478499

479500
class SpooledTempTests(unittest.TestCase):
480501
def test_in_memory_spooled_temp(self):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy