From f303c0ddeb33d418f810d0c59f116693f208f62a Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 17 Nov 2021 20:45:39 +0100 Subject: [PATCH] Fix several bugs with multiline comments Empty the current line if it's entirely inside a comment. Don't incorrectly end a block comment at the second line if it doesn't contain `*/`. Recognize `/*` to start a multiline comment even if it isn't at the start of the line. When stripping off comments, consistently strip off `/*` and `*/`. Signed-off-by: Gilles Peskine --- tests/scripts/check_names.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tests/scripts/check_names.py b/tests/scripts/check_names.py index 164d730229..5737cd6fdf 100755 --- a/tests/scripts/check_names.py +++ b/tests/scripts/check_names.py @@ -478,10 +478,15 @@ class CodeParser(): * in_block_comment indicates whether the line ends inside a block comment that continues on the next line. """ - # Terminate current comment? + + # Terminate current multiline comment? if in_block_comment: - line = re.sub(r".*?\*/", r"", line, 1) - in_block_comment = False + m = re.search(r"\*/", line) + if m: + in_block_comment = False + line = line[m.end(0):] + else: + return '', True # Remove full comments and string literals. # Do it all together to handle cases like "/*" correctly. @@ -492,10 +497,10 @@ class CodeParser(): # Start an unfinished comment? # (If `/*` was part of a complete comment, it's already been removed.) - m = re.match(r"/\*", line) + m = re.search(r"/\*", line) if m: in_block_comment = True - line = line[:m.end(0)] + line = line[:m.start(0)] return line, in_block_comment