Skip to content

Commit

Permalink
Merge pull request Ericsson#2671 from gyorb/invalid-plist-exception
Browse files Browse the repository at this point in the history
handle plistlib.InvalidFileException during parsing
  • Loading branch information
csordasmarton authored Apr 8, 2020
2 parents b3fc0e4 + 0dad286 commit b64d980
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
32 changes: 22 additions & 10 deletions codechecker_common/plist_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,21 @@ def parse_plist(plist_file_obj):
importlib.import_module('lxml')
parser = LXMLPlistParser()
return parser.parse(plist_file_obj)
except (ExpatError, TypeError, AttributeError) as err:
LOG.warning('Invalid plist file')
LOG.warning(err)
return
except ImportError:
LOG.debug("lxml library is not available. Use plistlib to parse plist "
"files.")
return plistlib.load(plist_file_obj)

try:
return plistlib.load(plist_file_obj)
except (ExpatError, TypeError, AttributeError, ValueError,
plistlib.InvalidFileException) as err:
LOG.warning('Invalid plist file')
LOG.warning(err)
return


def get_checker_name(diagnostic, path=""):
Expand All @@ -133,10 +144,12 @@ def parse_plist_file(path, source_root=None, allow_plist_update=True):
reports = []
files = []
try:
plist = None
with open(path, 'rb') as plist_file_obj:
plist = parse_plist(plist_file_obj)

if not plist:
LOG.error("Failed to parse plist %s", path)
return files, reports

files = plist['files']
Expand Down Expand Up @@ -188,10 +201,6 @@ def parse_plist_file(path, source_root=None, allow_plist_update=True):
# This way the client will always send a plist file where the
# report hash field is filled.
plistlib.dump(plist, path)
except (ExpatError, TypeError, AttributeError) as err:
LOG.warning('Failed to process plist file: %s wrong file format?',
path)
LOG.warning(err)
except IndexError as iex:
LOG.warning('Indexing error during processing plist file %s', path)
LOG.warning(type(iex))
Expand Down Expand Up @@ -301,11 +310,12 @@ def remove_report_from_plist(plist_file_obj, skip_handler):
report_data = None
try:
report_data = parse_plist(plist_file_obj)
except (ExpatError, TypeError, AttributeError) as ex:
LOG.error("Failed to parse plist content, "
"keeping the original version")
if not report_data:
return
except Exception as ex:
LOG.error("Plist parsing error")
LOG.error(ex)
return None
return

file_ids_to_remove = []

Expand All @@ -324,7 +334,7 @@ def remove_report_from_plist(plist_file_obj, skip_handler):
except KeyError:
LOG.error("Failed to modify plist content, "
"keeping the original version")
return None
return


def skip_report_from_plist(plist_file, skip_handler):
Expand All @@ -339,3 +349,5 @@ def skip_report_from_plist(plist_file, skip_handler):
if new_plist_content:
with open(plist_file, 'wb') as plist:
plist.write(new_plist_content)
else:
LOG.error("Failed to skip report from the plist file: %s", plist_file)
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def replace_report_hash(plist_file, hash_type=HashType.CONTEXT_FREE):

plistlib.dump(plist, pfile)

except (TypeError, AttributeError) as err:
except (TypeError, AttributeError, plistlib.InvalidFileException) as err:
LOG.warning('Failed to process plist file: %s wrong file format?',
plist_file)
LOG.warning(err)
Expand Down
2 changes: 1 addition & 1 deletion tools/plist_to_html/plist_to_html/PlistToHtml.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ def plist_to_html(file_path, output_path, html_builder,
print('Html file was generated: {0}'.format(html_output_path))
return None, changed_source

except ExpatError as err:
except (ExpatError, plistlib.InvalidFileException) as err:
print('Failed to process plist file: ' + file_path +
' wrong file format?', err)
return file_path, changed_source
Expand Down

0 comments on commit b64d980

Please sign in to comment.