From 973bb164eabef4877d5f2534d693be048c3c159e Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Mon, 20 Jul 2009 22:52:27 +0300 Subject: [PATCH 1/2] Fix ticket #486 (C-style pointer casting misses const pointers) http://sourceforge.net/apps/trac/cppcheck/ticket/486 --- src/checkother.cpp | 10 +++++++--- test/testother.cpp | 13 +++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/checkother.cpp b/src/checkother.cpp index a69b30cef..3d5634c23 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -45,14 +45,18 @@ void CheckOther::warningOldStylePointerCast() for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { // Old style pointer casting.. - if (!Token::Match(tok, "( %type% * ) %var%")) + if (!Token::Match(tok, "( const| %type% * ) %var%")) continue; - if (Token::simpleMatch(tok->tokAt(4), "const")) + int addToIndex = 0; + if (Token::simpleMatch(tok->tokAt(1), "const")) + addToIndex = 1; + + if (Token::simpleMatch(tok->tokAt(4 + addToIndex), "const")) continue; // Is "type" a class? - const std::string pattern("class " + tok->next()->str()); + const std::string pattern("class " + tok->tokAt(1 + addToIndex)->str()); if (!Token::findmatch(_tokenizer->tokens(), pattern.c_str())) continue; diff --git a/test/testother.cpp b/test/testother.cpp index 75ae08ff7..81ff4397b 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -517,6 +517,12 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:4]: (style) C-style pointer casting\n", errout.str()); + checkOldStylePointerCast("class Base;\n" + "void foo()\n" + "{\n" + " Base * b = (const Base *) derived;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (style) C-style pointer casting\n", errout.str()); checkOldStylePointerCast("class B;\n" "class A\n" @@ -524,6 +530,13 @@ private: " virtual void abc(B *) const = 0;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + checkOldStylePointerCast("class B;\n" + "class A\n" + "{\n" + " virtual void abc(const B *) const = 0;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } }; From d2278b5ce0d23b864cd99a998866b45e1bdf257b Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Mon, 20 Jul 2009 23:24:23 +0300 Subject: [PATCH 2/2] Fix some of the issues from #478 (Warnings in Visual Studio build with -W4) http://sourceforge.net/apps/trac/cppcheck/ticket/478 --- src/checkmemoryleak.cpp | 5 ++--- src/errorlogger.cpp | 4 ++-- src/preprocessor.cpp | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index 529a783ae..4af712cff 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -945,9 +945,8 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list(iss.get()); temp.append(1, c); } @@ -94,7 +94,7 @@ bool ErrorLogger::ErrorMessage::deserialize(const std::string &data) std::string temp; for (unsigned int i = 0; i < len && iss.good(); ++i) { - char c = iss.get(); + char c = static_cast(iss.get()); temp.append(1, c); } diff --git a/src/preprocessor.cpp b/src/preprocessor.cpp index b86f60f30..28ef9c675 100644 --- a/src/preprocessor.cpp +++ b/src/preprocessor.cpp @@ -286,7 +286,7 @@ std::string Preprocessor::read(std::istream &istr) if (ch == '\\') { char chNext = 0; - while (true) + for (;;) { chNext = (char)istr.peek(); if (chNext != '\n' && chNext != '\r' && (chNext > 0) &&