From a2be569f150ab3f3ab62323e89833fbe2c206dd9 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Thu, 9 May 2019 15:11:44 +0200 Subject: [PATCH] msodde: ensure XlsFile is always closed --- oletools/msodde.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/oletools/msodde.py b/oletools/msodde.py index ffb55d7..1a3a275 100644 --- a/oletools/msodde.py +++ b/oletools/msodde.py @@ -493,17 +493,23 @@ def process_xls(filepath): """ find dde links in excel ole file """ result = [] - for stream in xls_parser.XlsFile(filepath).iter_streams(): - if not isinstance(stream, xls_parser.WorkbookStream): - continue - for record in stream.iter_records(): - if not isinstance(record, xls_parser.XlsRecordSupBook): + xls_file = None + try: + xls_file = xls_parser.XlsFile(filepath) + for stream in xls_file.iter_streams(): + if not isinstance(stream, xls_parser.WorkbookStream): continue - if record.support_link_type in ( - xls_parser.XlsRecordSupBook.LINK_TYPE_OLE_DDE, - xls_parser.XlsRecordSupBook.LINK_TYPE_EXTERNAL): - result.append(record.virt_path.replace(u'\u0003', u' ')) - return u'\n'.join(result) + for record in stream.iter_records(): + if not isinstance(record, xls_parser.XlsRecordSupBook): + continue + if record.support_link_type in ( + xls_parser.XlsRecordSupBook.LINK_TYPE_OLE_DDE, + xls_parser.XlsRecordSupBook.LINK_TYPE_EXTERNAL): + result.append(record.virt_path.replace(u'\u0003', u' ')) + return u'\n'.join(result) + finally: + if xls_file is not None: + xls_file.close() def process_docx(filepath, field_filter_mode=None): -- libgit2 0.21.4