From 237dcb953307971757753258f30f61d22acda0fe Mon Sep 17 00:00:00 2001 From: Slava Semushin Date: Sat, 10 Oct 2009 19:26:03 +0700 Subject: [PATCH] Fixed #809 (Tokenizer: don't replace sizeof() of unknown type to zero) http://sourceforge.net/apps/trac/cppcheck/ticket/809 --- src/tokenize.cpp | 8 +++++++- test/testsimplifytokens.cpp | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 81ad1158c..f37287d54 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -1435,7 +1435,13 @@ void Tokenizer::simplifySizeof() Token::Match(tok->tokAt(-2), "[;{}(,] %type% %var% [;),]") || Token::Match(tok->tokAt(-3), "[;{}(,] const %type% %var% [;),]")) { - sizeOfVar[varId] = MathLib::toString(sizeOfType(tok->tokAt(-1))); + const int size = sizeOfType(tok->previous()); + if (size <= 0) + { + continue; + } + + sizeOfVar[varId] = MathLib::toString(size); } else if (Token::Match(tok->tokAt(-1), "%type% %var% [ %num% ] [;=]") || diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 8f822960a..9fc7d33c3 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -65,6 +65,7 @@ private: TEST_CASE(sizeof7); TEST_CASE(sizeof8); TEST_CASE(sizeof9); + TEST_CASE(sizeof10); TEST_CASE(casting); TEST_CASE(template1); @@ -775,6 +776,16 @@ private: } } + void sizeof10() + { + // ticket #809 + const char code[] = "int m ; " + "compat_ulong_t um ; " + "size_t size ; size = sizeof ( m ) / sizeof ( um ) ;"; + + ASSERT_EQUALS(code, tok(code)); + } + void casting() { {