Skip to content

Commit

Permalink
gcc parser: extract CWE metadata (#43)
Browse files Browse the repository at this point in the history
Signed-off-by: David Malcolm <dmalcolm@redhat.com>
  • Loading branch information
davidmalcolm committed Dec 1, 2021
1 parent ec2958e commit 92cc608
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
16 changes: 14 additions & 2 deletions firehose/parsers/gcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

SWITCH_SUB_PATTERN = re.compile("^ \[\-W(?P<name>.*)\]$")

CWE_SUB_PATTERN = re.compile("^(?P<message>.*) \[CWE-(?P<cwe>[0-9]+)\]$")

# single quotes may not match locales that are not C
FUNCTION_PATTERN = re.compile(".*: In (?:member )?function '(?P<func>.*)':")

Expand Down Expand Up @@ -107,7 +109,17 @@ def parse_warning(line, func_name):
"""
match = GCC_PATTERN.match(line)
if match:
message = Message(match.group('message'))
text = match.group('message')
# GCC 10 onwards can (optionally) append a CWE id to the message.
# Extract it if it is present.
cwe_match = CWE_SUB_PATTERN.match(text)
if cwe_match:
message = Message(cwe_match.group('message'))
cwe = int(cwe_match.group('cwe'))
else:
message = Message(text)
cwe = None

func = Function(func_name)
try:
column = int(match.group('column'))
Expand All @@ -128,7 +140,7 @@ def parse_warning(line, func_name):
path = File(match.group('path'), None)
location = Location(path, func, point)

return Issue(None, switch, location, message, None, None)
return Issue(cwe, switch, location, message, None, None)


if __name__ == '__main__':
Expand Down
10 changes: 10 additions & 0 deletions tests/parsers/test_gcc_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ def test_parse_no_switch(self):
issue = gcc.parse_warning(line, FUNC_NAME)
self.assertIsInstance(issue, Issue)

def test_cwe(self):
line = "CWE415_Double_Free__malloc_free_long_16.c:40:9: warning: double-'free' of 'data' [CWE-415] [-Wanalyzer-double-free]"
issue = gcc.parse_warning(line, FUNC_NAME)
self.assertIsInstance(issue, Issue)
self.assertEqual(issue.testid, 'analyzer-double-free')
self.assertEqual(issue.location.file.givenpath, 'CWE415_Double_Free__malloc_free_long_16.c')
self.assertEqual(issue.location.point.line, 40)
self.assertEqual(issue.location.point.column, 9)
self.assertEqual(issue.message.text, "double-'free' of 'data'")
self.assertEqual(issue.cwe, 415)

class TestParseFile(unittest.TestCase):
@staticmethod
Expand Down

0 comments on commit 92cc608

Please sign in to comment.