From 9750a9157143fd43a1becebbcdcb2b0635bd3447 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 23 Feb 2018 14:18:52 +0100 Subject: [PATCH] Fixed file leak when opening corrupted file. --- blender_asset_tracer/blendfile/__init__.py | 12 ++++++++---- tests/blendfiles/corrupt_only_magic.blend | Bin 0 -> 36 bytes tests/test_blendfile_loading.py | 7 +++++-- 3 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 tests/blendfiles/corrupt_only_magic.blend diff --git a/blender_asset_tracer/blendfile/__init__.py b/blender_asset_tracer/blendfile/__init__.py index 361efe6..5335678 100644 --- a/blender_asset_tracer/blendfile/__init__.py +++ b/blender_asset_tracer/blendfile/__init__.py @@ -97,15 +97,19 @@ class BlendFile: fileobj.close() raise exceptions.BlendFileError("File is not a blend file", path) - self.header = header.BlendFileHeader(self.fileobj, self.raw_filepath) - self.block_header_struct = self.header.create_block_header_struct() self.blocks = [] self.code_index = collections.defaultdict(list) self.structs = [] self.sdna_index_from_id = {} self.block_from_addr = {} - self._load_blocks() + try: + self.header = header.BlendFileHeader(self.fileobj, self.raw_filepath) + self.block_header_struct = self.header.create_block_header_struct() + self._load_blocks() + except Exception: + fileobj.close() + raise def _load_blocks(self): """Read the blend file to load its DNA structure to memory.""" @@ -304,7 +308,7 @@ class BlendFileBlock: if len(data) != header_struct.size: self.log.warning("Blend file %s seems to be truncated, " "expected %d bytes but could read only %d", - header_struct.size, len(data)) + bfile.filepath, header_struct.size, len(data)) self.code = b'ENDB' return diff --git a/tests/blendfiles/corrupt_only_magic.blend b/tests/blendfiles/corrupt_only_magic.blend new file mode 100644 index 0000000000000000000000000000000000000000..feedba8726fcef978be6410c3969afce8c8013bb GIT binary patch literal 36 YcmZ?rarJX?4bm+$GPeY>oEWeG0DP$gTL1t6 literal 0 HcmV?d00001 diff --git a/tests/test_blendfile_loading.py b/tests/test_blendfile_loading.py index 2baeccc..3cae760 100644 --- a/tests/test_blendfile_loading.py +++ b/tests/test_blendfile_loading.py @@ -1,6 +1,5 @@ import os import pathlib -import unittest from blender_asset_tracer import blendfile from blender_asset_tracer.blendfile import iterators, exceptions @@ -210,7 +209,11 @@ class LoadCompressedTest(AbstractBlendFileTest): self.assertEqual('OBümlaut', name) -class LoadNonBlendfileTest(unittest.TestCase): +class LoadNonBlendfileTest(AbstractBlendFileTest): def test_loading(self): with self.assertRaises(exceptions.BlendFileError): blendfile.BlendFile(pathlib.Path(__file__)) + + def test_no_datablocks(self): + with self.assertRaises(exceptions.NoDNA1Block): + blendfile.BlendFile(self.blendfiles / 'corrupt_only_magic.blend')