From 0a7da96cb6b9964ebede6d2be99681442045c0ad Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Tue, 6 Oct 2009 09:14:59 +0300 Subject: [PATCH] Fix #765 (divsion by zero not detected in std::cout stream) http://sourceforge.net/apps/trac/cppcheck/ticket/765 --- src/tokenize.cpp | 9 +++++++-- test/testtokenize.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index a35e4c6ea..9edee75d4 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -3269,7 +3269,9 @@ bool Tokenizer::simplifyKnownVariables() } // Variable is used in calculation.. - if (Token::Match(tok3, "[=+-*/[] %varid% [+-*/;]]", varid)) + if (Token::Match(tok3, "[=+-*/[] %varid% [+-*/;]]", varid) || + Token::Match(tok3, "[=+-*/[] %varid% <<", varid) || + Token::Match(tok3, "<< %varid% [+-*/;]]", varid)) { tok3 = tok3->next(); tok3->str(value); @@ -3428,7 +3430,10 @@ bool Tokenizer::simplifyCalculations() } // (1-2) - if (Token::Match(tok, "[[,(=<>+-*] %num% [+-*/] %num% [],);=<>+-*/]")) + if (Token::Match(tok, "[[,(=<>+-*] %num% [+-*/] %num% [],);=<>+-*/]") || + Token::Match(tok, "<< %num% [+-*/] %num% [],);=<>+-*/]") || + Token::Match(tok, "[[,(=<>+-*] %num% [+-*/] %num% <<") || + Token::Match(tok, "<< %num% [+-*/] %num% <<")) { tok = tok->next(); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 091906a78..875fa21c6 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -81,6 +81,7 @@ private: TEST_CASE(simplifyKnownVariables12); TEST_CASE(simplifyKnownVariables13); TEST_CASE(simplifyKnownVariables14); + TEST_CASE(simplifyKnownVariables15); TEST_CASE(match1); @@ -815,6 +816,32 @@ private: ASSERT_EQUALS(code, simplifyKnownVariables(code)); } + void simplifyKnownVariables15() + { + { + const char code[] = "int main()\n" + "{\n" + " int x=5;\n" + " std::cout << 10 / x << std::endl;\n" + "}\n"; + + ASSERT_EQUALS( + "int main ( ) { int x ; x = 5 ; std :: cout << 10 / 5 << std :: endl ; }", + simplifyKnownVariables(code)); + } + + { + const char code[] = "int main()\n" + "{\n" + " int x=5;\n" + " std::cout << x / ( x == 1 ) << std::endl;\n" + "}\n"; + + ASSERT_EQUALS( + "int main ( ) { int x ; x = 5 ; std :: cout << 5 / ( 5 == 1 ) << std :: endl ; }", + simplifyKnownVariables(code)); + } + } void match1() {