From 2939c420ed9f2697bcb82c1b193bb48c4a8ad7a9 Mon Sep 17 00:00:00 2001 From: Mark VanderVoord Date: Mon, 12 Aug 2019 15:40:43 -0400 Subject: [PATCH] Better protection against nested comments (and things that look like comments) --- auto/generate_test_runner.rb | 8 ++++---- test/testdata/testRunnerGenerator.c | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/auto/generate_test_runner.rb b/auto/generate_test_runner.rb index dab5096..6dc90e0 100644 --- a/auto/generate_test_runner.rb +++ b/auto/generate_test_runner.rb @@ -104,10 +104,10 @@ class UnityTestRunnerGenerator source_scrubbed = source_scrubbed.gsub(/\\"/, '@quote@') # hide escaped quotes to allow capture of the full string/char source_scrubbed = source_scrubbed.gsub(/\\'/, '@apos@') # hide escaped apostrophes to allow capture of the full string/char source_scrubbed = source_scrubbed.gsub(/("[^"\n]*")|('[^'\n]*')/) { |s| s.gsub(substring_re, substring_subs) } # temporarily hide problematic characters within strings - source_scrubbed = source_scrubbed.gsub(/\/\/.*$/, '') # remove line comments - source_scrubbed = source_scrubbed.gsub(/\/\*.*?\*\//m, '') # remove block comments - lines = source_scrubbed.split(/(^\s*\#.*$) # Treat preprocessor directives as a logical line - | (;|\{|\}) /x) # Match ;, {, and } as end of lines + source_scrubbed = source_scrubbed.gsub(/\/\/(?:.+\/\*|\*(?:$|[^\/])).*$/, '') # remove line comments that comment out the start of blocks + source_scrubbed = source_scrubbed.gsub(/\/\*.*?\*\//m, '') # remove block comments + source_scrubbed = source_scrubbed.gsub(/\/\/.*$/, '') # remove line comments (all that remain) + lines = source_scrubbed.split(/(^\s*\#.*$) | (;|\{|\}) /x) # Treat preprocessor directives as a logical line. Match ;, {, and } as end of lines .map { |line| line.gsub(substring_unre, substring_unsubs) } # unhide the problematic characters previously removed lines.each_with_index do |line, _index| diff --git a/test/testdata/testRunnerGenerator.c b/test/testdata/testRunnerGenerator.c index b3c0cdd..1139939 100644 --- a/test/testdata/testRunnerGenerator.c +++ b/test/testdata/testRunnerGenerator.c @@ -124,7 +124,9 @@ void test_NotBeConfusedByLongComplicatedStrings(void) void test_NotDisappearJustBecauseTheTestBeforeAndAfterHaveCrazyStrings(void) { TEST_ASSERT_TRUE_MESSAGE(1, "1 Should be True"); + /* still should not break anything */ } +/* nor should this */ void test_StillNotBeConfusedByLongComplicatedStrings(void) { -- GitLab