Fix for KeyError: 'region', caused by result without region

Region / line / column information are present in most messages.  The one that
caused this error refers to the whole file:

    ipdb> p sarif_struct

    {'ruleId': 'com.lgtm/cpp-queries:cpp/missing-header-guard', 'ruleIndex': 12,
    'message': {'text': 'This header file should contain a header guard to prevent
    multiple inclusion.'}, 'locations': [{'physicalLocation': {'artifactLocation':
    {'uri': 'diff/cmpbuf.h', 'uriBaseId': '%SRCROOT%', 'index': 13}}}],
    'partialFingerprints': {'primaryLocationLineHash': 'd04cb834fa64727d:1',
    'primaryLocationStartColumnFingerprint': '0'}}

The goal is fixed-structure output formatting, so whole-file output uses
-1,-1,-1,-1 for line, column information.
This commit is contained in:
Michael Hohn
2021-12-06 11:27:39 -08:00
committed by =Michael Hohn
parent ffcacec630
commit 2c3ca3c0eb
2 changed files with 15 additions and 3 deletions

View File

@@ -33,13 +33,17 @@ for runi in S.indices(sarif_struct, 'runs'):
# Non-path problems # Non-path problems
# TODO: just pull out the uri, not the artifact # TODO: just pull out the uri, not the artifact
message, artifact, region = S.get_location_message_info(result) message, artifact, region = S.get_location_message_info(result)
l1, c1, l2, c2 = S.lineinfo(region) if region == S.WholeFile:
l1, c1, l2, c2 = -1, -1, -1, -1
else:
l1, c1, l2, c2 = S.lineinfo(region)
filepath = "%s:%d:%d:%d:%d" % (artifact['uri'], l1, c1, l2, c2) filepath = "%s:%d:%d:%d:%d" % (artifact['uri'], l1, c1, l2, c2)
if args.csv: if args.csv:
S.write_csv(cw, "result", artifact['uri'], l1, c1, l2, c2, message) S.write_csv(cw, "result", artifact['uri'], l1, c1, l2, c2, message)
else: else:
S.msg("RESULT: %s: %s\n\n" % (filepath, message)) S.msg("RESULT: %s: %s\n\n" % (filepath, message))
if args.list_source:
if region != S.WholeFile and args.list_source:
lines = S.load_lines(args.list_source, artifact['uri'], l1, l2) lines = S.load_lines(args.list_source, artifact['uri'], l1, l2)
if args.csv: if args.csv:
pass pass

View File

@@ -33,14 +33,22 @@ def get_relatedlocation_message_info(related_location):
region = get(related_location, 'physicalLocation', 'region') region = get(related_location, 'physicalLocation', 'region')
return message, artifact, region return message, artifact, region
class WholeFile:
pass
def get_location_message_info(result): def get_location_message_info(result):
""" Given one of the results, extract message information. """ Given one of the results, extract message information.
The `result` typically starts from get(sarif_struct, 'runs', run_index, 'results', res_index) The `result` typically starts from get(sarif_struct, 'runs', run_index, 'results', res_index)
Returns: (message, artifact, region)
For an empty 'region' key, returns (message, artifact, sarif_cli.WholeFile)
""" """
message = get(result, 'message', 'text') message = get(result, 'message', 'text')
artifact = get(result, 'locations', 0, 'physicalLocation', 'artifactLocation') artifact = get(result, 'locations', 0, 'physicalLocation', 'artifactLocation')
region = get(result, 'locations', 0, 'physicalLocation', 'region') # If there is no 'region' key, use the whole file
region = get(result, 'locations', 0, 'physicalLocation').get('region', WholeFile)
return (message, artifact, region) return (message, artifact, region)
def display_underlined(l1, c1, l2, c2, line, line_num): def display_underlined(l1, c1, l2, c2, line, line_num):