Fix comment parsing

Fix cases like
```
/*short comment*/ /*long
 comment */
int mbedtls_foo;
```
where the previous code thought that the second line started outside of a
comment and ended inside of a comment.

I believe that the new code strips comments correctly. It also strips string
literals, just in case.

Fixes #5191.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2021-11-16 20:48:59 +01:00
parent c0656b43f1
commit 7493c4017a

View File

@ -509,18 +509,19 @@ class CodeParser():
previous_line = "" previous_line = ""
for line_no, line in enumerate(header): for line_no, line in enumerate(header):
# Skip parsing this line if a block comment ends on it, # Terminate current comment?
# but don't skip if it has just started -- there is a chance
# it ends on the same line.
if re.search(r"/\*", line):
in_block_comment = not in_block_comment
if re.search(r"\*/", line):
in_block_comment = not in_block_comment
continue
if in_block_comment: if in_block_comment:
previous_line = "" line = re.sub(r".*?\*/", r"", line, 1)
continue in_block_comment = False
# Remove full comments and string literals
line = re.sub(r'/\*.*?\*/|(")(?:[^\\\"]|\\.)*"',
lambda s: '""' if s.group(1) else ' ',
line)
# Start an unfinished comment?
m = re.match(r"/\*", line)
if m:
in_block_comment = True
line = line[:m.end(0)]
if exclusion_lines.search(line): if exclusion_lines.search(line):
previous_line = "" previous_line = ""