From e856bce4e99fb37810534f213b9466d6e19b0058 Mon Sep 17 00:00:00 2001 From: Duong Do Minh Chau Date: Sun, 26 Apr 2020 20:40:16 +0700 Subject: [PATCH 1/2] Treat ' as digit separator when file extension is .cpp, .cxx, .cc --- flawfinder | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/flawfinder b/flawfinder index 60a2c1a..cee78ee 100755 --- a/flawfinder +++ b/flawfinder @@ -1461,6 +1461,7 @@ def process_c_file(f, patch_infos): linenumber = 1 ignoreline = -1 + cpplanguage = f.endswith(".cpp") or f.endswith(".cxx") or f.endswith(".cc") incomment = 0 instring = 0 linebegin = 1 @@ -1644,9 +1645,12 @@ def process_c_file(f, patch_infos): startpos + max_lookahead] hit.hook(hit) elif p_digits.match(c): - while i < len(text) and p_digits.match( - text[i]): # Process a number. - i += 1 + while i < len(text): # Process a number. + # C does not have digit separator + if p_digits.match(text[i]) or (cpplanguage and text[i] == "'"): + i += 1 + else: + break # else some other character, which we ignore. # End of loop through text. Wrap up. if codeinline: From 1bb1a69d6c816edf17f2af2b2468495adffb5235 Mon Sep 17 00:00:00 2001 From: Duong Do Minh Chau Date: Mon, 27 Apr 2020 20:22:27 +0700 Subject: [PATCH 2/2] Add test --- test/makefile | 12 +++++++++++- test/test-cpp-digit-separator.c | 1 + test/test-cpp-digit-separator.cpp | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 test/test-cpp-digit-separator.c create mode 100644 test/test-cpp-digit-separator.cpp diff --git a/test/makefile b/test/makefile index b4d4c5a..3850b2f 100644 --- a/test/makefile +++ b/test/makefile @@ -66,11 +66,21 @@ test_008: $(FLAWFINDER) test.c test-results-008.txt @diff -u correct-results-008.txt test-results-008.txt +test_009: $(FLAWFINDER) test-cpp-digit-separator.cpp + @echo 'test_009 (C++ digit separator)' +# C++ file should have no problem recognizing ' as digit separator + @$(PYTHON) $(FLAWFINDER) test-cpp-digit-separator.cpp > /dev/null +# C file should fail with unterminated char literal error + @$(PYTHON) $(FLAWFINDER) test-cpp-digit-separator.c 2>&1 \ + | grep 'File ended while in string.' \ + > /dev/null + # Run all tests on *one* version of Python; # output shows differences from expected results. # If everything works as expected, it just prints test numbers. # Set PYTHON as needed, including to "" -test: test_001 test_002 test_003 test_004 test_005 test_006 test_007 test_008 +test: test_001 test_002 test_003 test_004 test_005 test_006 test_007 test_008 \ + test_009 @echo 'All tests pass!' # Usual check routine. Run all tests using *both* python2 and python3. diff --git a/test/test-cpp-digit-separator.c b/test/test-cpp-digit-separator.c new file mode 100644 index 0000000..05232ef --- /dev/null +++ b/test/test-cpp-digit-separator.c @@ -0,0 +1 @@ +int main() { return 0'000; } \ No newline at end of file diff --git a/test/test-cpp-digit-separator.cpp b/test/test-cpp-digit-separator.cpp new file mode 100644 index 0000000..05232ef --- /dev/null +++ b/test/test-cpp-digit-separator.cpp @@ -0,0 +1 @@ +int main() { return 0'000; } \ No newline at end of file