From 05da4a559fdd17cb4bac73955a638aef749d2c33 Mon Sep 17 00:00:00 2001 From: Wolf-Bastian Poettner Date: Sat, 1 Feb 2020 20:13:05 +0000 Subject: [PATCH] Avoid TOCTOU error by rather catching an exception than checking beforehand --- src/documents/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/documents/models.py b/src/documents/models.py index 25caddebf..c52b6ba0a 100644 --- a/src/documents/models.py +++ b/src/documents/models.py @@ -450,9 +450,9 @@ def delete_files(sender, instance, **kwargs): # Remove the document old_file = instance.filename_to_path(instance.filename) - if os.path.isfile(old_file): + try: os.remove(old_file) - else: + except FileNotFoundError: logger = logging.getLogger(__name__) logger.warning("Deleted document " + str(instance.id) + " but file " + old_file + " was no longer present")