From 7493c4017a95d82110a6c05fd331fcae3a56c200 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Tue, 16 Nov 2021 20:48:59 +0100 Subject: [PATCH] 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 --- tests/scripts/check_names.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/tests/scripts/check_names.py b/tests/scripts/check_names.py index cae722e58..32eaf2164 100755 --- a/tests/scripts/check_names.py +++ b/tests/scripts/check_names.py @@ -509,18 +509,19 @@ class CodeParser(): previous_line = "" for line_no, line in enumerate(header): - # Skip parsing this line if a block comment ends on it, - # 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 - + # Terminate current comment? if in_block_comment: - previous_line = "" - continue + line = re.sub(r".*?\*/", r"", line, 1) + 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): previous_line = ""