Commit 8f68844bc2634b0adcbd169a9406c55dc048f669

Authored by Christian Herdtweck
1 parent a58b7439

olevba[3]: ensure file handles are closed

Trying to get rid of all those ResourceWarnings that python3 shows during
unittests
oletools/olevba.py
@@ -2336,7 +2336,8 @@ class VBA_Parser(object): @@ -2336,7 +2336,8 @@ class VBA_Parser(object):
2336 # read file from disk, check if it is a Word 2003 XML file (WordProcessingML), Excel 2003 XML, 2336 # read file from disk, check if it is a Word 2003 XML file (WordProcessingML), Excel 2003 XML,
2337 # or a plain text file containing VBA code 2337 # or a plain text file containing VBA code
2338 if data is None: 2338 if data is None:
2339 - data = open(filename, 'rb').read() 2339 + with open(filename, 'rb') as file_handle:
  2340 + data = file_handle.read()
2340 # check if it is a Word 2003 XML file (WordProcessingML): must contain the namespace 2341 # check if it is a Word 2003 XML file (WordProcessingML): must contain the namespace
2341 if b'http://schemas.microsoft.com/office/word/2003/wordml' in data: 2342 if b'http://schemas.microsoft.com/office/word/2003/wordml' in data:
2342 self.open_word2003xml(data) 2343 self.open_word2003xml(data)
@@ -2398,10 +2399,12 @@ class VBA_Parser(object): @@ -2398,10 +2399,12 @@ class VBA_Parser(object):
2398 #TODO: if the zip file is encrypted, suggest to use the -z option, or try '-z infected' automatically 2399 #TODO: if the zip file is encrypted, suggest to use the -z option, or try '-z infected' automatically
2399 # check each file within the zip if it is an OLE file, by reading its magic: 2400 # check each file within the zip if it is an OLE file, by reading its magic:
2400 for subfile in z.namelist(): 2401 for subfile in z.namelist():
2401 - magic = z.open(subfile).read(len(olefile.MAGIC)) 2402 + with z.open(subfile) as file_handle:
  2403 + magic = file_handle.read(len(olefile.MAGIC))
2402 if magic == olefile.MAGIC: 2404 if magic == olefile.MAGIC:
2403 log.debug('Opening OLE file %s within zip' % subfile) 2405 log.debug('Opening OLE file %s within zip' % subfile)
2404 - ole_data = z.open(subfile).read() 2406 + with z.open(subfile) as file_handle:
  2407 + ole_data = file_handle.read()
2405 try: 2408 try:
2406 self.ole_subfiles.append( 2409 self.ole_subfiles.append(
2407 VBA_Parser(filename=subfile, data=ole_data, 2410 VBA_Parser(filename=subfile, data=ole_data,
oletools/olevba3.py
@@ -2289,7 +2289,8 @@ class VBA_Parser(object): @@ -2289,7 +2289,8 @@ class VBA_Parser(object):
2289 # read file from disk, check if it is a Word 2003 XML file (WordProcessingML), Excel 2003 XML, 2289 # read file from disk, check if it is a Word 2003 XML file (WordProcessingML), Excel 2003 XML,
2290 # or a plain text file containing VBA code 2290 # or a plain text file containing VBA code
2291 if data is None: 2291 if data is None:
2292 - data = open(filename, 'rb').read() 2292 + with open(filename, 'rb') as file_handle:
  2293 + data = file_handle.read()
2293 # check if it is a Word 2003 XML file (WordProcessingML): must contain the namespace 2294 # check if it is a Word 2003 XML file (WordProcessingML): must contain the namespace
2294 if b'http://schemas.microsoft.com/office/word/2003/wordml' in data: 2295 if b'http://schemas.microsoft.com/office/word/2003/wordml' in data:
2295 self.open_word2003xml(data) 2296 self.open_word2003xml(data)
@@ -2351,10 +2352,12 @@ class VBA_Parser(object): @@ -2351,10 +2352,12 @@ class VBA_Parser(object):
2351 #TODO: if the zip file is encrypted, suggest to use the -z option, or try '-z infected' automatically 2352 #TODO: if the zip file is encrypted, suggest to use the -z option, or try '-z infected' automatically
2352 # check each file within the zip if it is an OLE file, by reading its magic: 2353 # check each file within the zip if it is an OLE file, by reading its magic:
2353 for subfile in z.namelist(): 2354 for subfile in z.namelist():
2354 - magic = z.open(subfile).read(len(olefile.MAGIC)) 2355 + with z.open(subfile) as file_handle:
  2356 + magic = file_handle.read(len(olefile.MAGIC))
2355 if magic == olefile.MAGIC: 2357 if magic == olefile.MAGIC:
2356 log.debug('Opening OLE file %s within zip' % subfile) 2358 log.debug('Opening OLE file %s within zip' % subfile)
2357 - ole_data = z.open(subfile).read() 2359 + with z.open(subfile) as file_handle:
  2360 + ole_data = file_handle.read()
2358 try: 2361 try:
2359 self.ole_subfiles.append( 2362 self.ole_subfiles.append(
2360 VBA_Parser(filename=subfile, data=ole_data, 2363 VBA_Parser(filename=subfile, data=ole_data,