Commit b52e0e448fef62e0b48d752b6d62c002d8653fa6

Authored by Christian Herdtweck
1 parent 2f6fb299

oleobj: Always close PptFile

Showing 1 changed file with 29 additions and 23 deletions
oletools/oleobj.py
@@ -526,29 +526,35 @@ def find_ole_in_ppt(filename): @@ -526,29 +526,35 @@ def find_ole_in_ppt(filename):
526 can contain the actual embedded file we are looking for (caller will check 526 can contain the actual embedded file we are looking for (caller will check
527 for these). 527 for these).
528 """ 528 """
529 - for stream in PptFile(filename).iter_streams():  
530 - for record_idx, record in enumerate(stream.iter_records()):  
531 - if isinstance(record, PptRecordExOleVbaActiveXAtom):  
532 - ole = None  
533 - try:  
534 - data_start = next(record.iter_uncompressed())  
535 - if data_start[:len(olefile.MAGIC)] != olefile.MAGIC:  
536 - continue # could be an ActiveX control or VBA Storage  
537 -  
538 - # otherwise, this should be an OLE object  
539 - log.debug('Found record with embedded ole object in ppt '  
540 - '(stream "{0}", record no {1})'  
541 - .format(stream.name, record_idx))  
542 - ole = record.get_data_as_olefile()  
543 - yield ole  
544 - except IOError:  
545 - log.warning('Error reading data from {0} stream or '  
546 - 'interpreting it as OLE object'  
547 - .format(stream.name))  
548 - log.debug('', exc_info=True)  
549 - finally:  
550 - if ole is not None:  
551 - ole.close() 529 + ppt_file = None
  530 + try:
  531 + ppt_file = PptFile(filename)
  532 + for stream in ppt_file.iter_streams():
  533 + for record_idx, record in enumerate(stream.iter_records()):
  534 + if isinstance(record, PptRecordExOleVbaActiveXAtom):
  535 + ole = None
  536 + try:
  537 + data_start = next(record.iter_uncompressed())
  538 + if data_start[:len(olefile.MAGIC)] != olefile.MAGIC:
  539 + continue # could be ActiveX control / VBA Storage
  540 +
  541 + # otherwise, this should be an OLE object
  542 + log.debug('Found record with embedded ole object in '
  543 + 'ppt (stream "{0}", record no {1})'
  544 + .format(stream.name, record_idx))
  545 + ole = record.get_data_as_olefile()
  546 + yield ole
  547 + except IOError:
  548 + log.warning('Error reading data from {0} stream or '
  549 + 'interpreting it as OLE object'
  550 + .format(stream.name))
  551 + log.debug('', exc_info=True)
  552 + finally:
  553 + if ole is not None:
  554 + ole.close()
  555 + finally:
  556 + if ppt_file is not None:
  557 + ppt_file.close()
552 558
553 559
554 class FakeFile(io.RawIOBase): 560 class FakeFile(io.RawIOBase):