From a0d6da506bf47c0ad2a02fd0a1c158ee5988ae7c Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Mon, 8 Mar 2010 22:40:50 +0200 Subject: [PATCH 01/11] Fix #1485 (cppcheck.exe 1.41 crashes when calling with option --errorlist) http://sourceforge.net/apps/trac/cppcheck/ticket/1485 --- lib/check.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/check.h b/lib/check.h index 8c9729425..997123672 100644 --- a/lib/check.h +++ b/lib/check.h @@ -118,6 +118,10 @@ protected: std::list locationList; for (std::list::const_iterator it = callstack.begin(); it != callstack.end(); ++it) { + // --errorlist can provide null values here + if (!(*it)) + continue; + ErrorLogger::ErrorMessage::FileLocation loc; loc.line = (*it)->linenr(); loc.file = _tokenizer->file(*it); From dbc235bd39d384eaa9dfa8b7c803c30b43af86fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 9 Mar 2010 07:29:03 +0100 Subject: [PATCH 02/11] Function constness: I don't think it's necessary to show both definition and implementation location. I changed so only the location of the definition is shown. This fixes #1486. --- lib/checkclass.cpp | 10 +--------- lib/checkclass.h | 2 -- test/testclass.cpp | 40 ++++++++++++++++++++-------------------- 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index a17b2faf3..48e268845 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1593,7 +1593,7 @@ void CheckClass::checkConst() for (int k = nestInfo.size() - 2; k >= 0; k--) classname = std::string(nestInfo[k].className + "::" + classname); - checkConstError2(found, tok2, classname, functionName); + checkConstError(found, classname, functionName); } } } @@ -1785,14 +1785,6 @@ void CheckClass::checkConstError(const Token *tok, const std::string &classname, reportError(tok, Severity::style, "functionConst", "The function '" + classname + "::" + funcname + "' can be const"); } -void CheckClass::checkConstError2(const Token *tok1, const Token *tok2, const std::string &classname, const std::string &funcname) -{ - std::list toks; - toks.push_back(tok1); - toks.push_back(tok2); - reportError(toks, Severity::style, "functionConst", "The function '" + classname + "::" + funcname + "' can be const"); -} - void CheckClass::noConstructorError(const Token *tok, const std::string &classname, bool isStruct) { reportError(tok, Severity::style, "noConstructor", "The " + std::string(isStruct ? "struct" : "class") + " '" + classname + "' has no constructor. Member variables not initialized."); diff --git a/lib/checkclass.h b/lib/checkclass.h index c5c4f7102..087d5512a 100644 --- a/lib/checkclass.h +++ b/lib/checkclass.h @@ -169,7 +169,6 @@ private: void operatorEqToSelfError(const Token *tok); void checkConstError(const Token *tok, const std::string &classname, const std::string &funcname); - void checkConstError2(const Token *tok1, const Token *tok2, const std::string &classname, const std::string &funcname); void getErrorMessages() { @@ -185,7 +184,6 @@ private: operatorEqRetRefThisError(0); operatorEqToSelfError(0); checkConstError(0, "class", "function"); - checkConstError2(0, 0, "class", "function"); } std::string name() const diff --git a/test/testclass.cpp b/test/testclass.cpp index 24f0f4795..3db64def9 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -1778,20 +1778,20 @@ private: " int getA();\n" "};\n" "int Fred::getA() { return a; }"); - ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::getA' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::getA' can be const\n", errout.str()); checkConst("class Fred {\n" " const std::string foo();\n" "};\n" "const std::string Fred::foo() { return ""; }"); - ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (style) The function 'Fred::foo' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (style) The function 'Fred::foo' can be const\n", errout.str()); checkConst("class Fred {\n" " std::string s;\n" " const std::string & foo();\n" "};\n" "const std::string & Fred::foo() { return ""; }"); - ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::foo' can be const\n", errout.str()); // constructors can't be const.. checkConst("class Fred {\n" @@ -1843,7 +1843,7 @@ private: " void foo(std::string & a);\n" "};\n" "void Fred::foo(std::string & a) { a = s; }"); - ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::foo' can be const\n", errout.str()); // assignment to variable can't be const checkConst("class Fred {\n" @@ -1859,7 +1859,7 @@ private: " void foo(std::string & a, std::string & b);\n" "};\n" "void Fred::foo(std::string & a, std::string & b) { a = s; b = s; }"); - ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::foo' can be const\n", errout.str()); // assignment to variable, can't be const checkConst("class Fred {\n" @@ -1891,7 +1891,7 @@ private: " void foo(int * a);\n" "};\n" "void Fred::foo(int * a) { *a = s; }"); - ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::foo' can be const\n", errout.str()); // assignment to variable, can't be const checkConst("class Fred {\n" @@ -1907,7 +1907,7 @@ private: " void foo(std::string * a, std::string * b);\n" "};\n" "void Fred::foo(std::string * a, std::string * b) { *a = s; *b = s; }"); - ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:5]: (style) The function 'Fred::foo' can be const\n", errout.str()); // assignment to variable, can't be const checkConst("class Fred {\n" @@ -1943,8 +1943,8 @@ private: "void Fred::foo() { }" "void Fred::foo(std::string & a) { a = s; }" "void Fred::foo(const std::string & a) { s = a; }"); - ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:3]: (style) The function 'Fred::foo' can be const\n" - "[test.cpp:7] -> [test.cpp:4]: (style) The function 'Fred::foo' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:7]: (style) The function 'Fred::foo' can be const\n" + "[test.cpp:7]: (style) The function 'Fred::foo' can be const\n", errout.str()); // check functions with different or missing paramater names checkConst("class Fred {\n" @@ -1960,11 +1960,11 @@ private: "void Fred::foo3(int a, int b) { }\n" "void Fred::foo4(int a, int b) { }\n" "void Fred::foo5(int, int) { }"); - ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:3]: (style) The function 'Fred::foo1' can be const\n" - "[test.cpp:10] -> [test.cpp:4]: (style) The function 'Fred::foo2' can be const\n" - "[test.cpp:11] -> [test.cpp:5]: (style) The function 'Fred::foo3' can be const\n" - "[test.cpp:12] -> [test.cpp:6]: (style) The function 'Fred::foo4' can be const\n" - "[test.cpp:13] -> [test.cpp:7]: (style) The function 'Fred::foo5' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:9]: (style) The function 'Fred::foo1' can be const\n" + "[test.cpp:10]: (style) The function 'Fred::foo2' can be const\n" + "[test.cpp:11]: (style) The function 'Fred::foo3' can be const\n" + "[test.cpp:12]: (style) The function 'Fred::foo4' can be const\n" + "[test.cpp:13]: (style) The function 'Fred::foo5' can be const\n", errout.str()); // check nested classes checkConst("class Fred {\n" @@ -1982,7 +1982,7 @@ private: " };\n" " int A::getA() { return a; }\n" "};"); - ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:4]: (style) The function 'Fred::A::getA' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:6]: (style) The function 'Fred::A::getA' can be const\n", errout.str()); checkConst("class Fred {\n" " class A {\n" @@ -1991,7 +1991,7 @@ private: " };\n" "};\n" "int Fred::A::getA() { return a; }"); - ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:4]: (style) The function 'Fred::A::getA' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:7]: (style) The function 'Fred::A::getA' can be const\n", errout.str()); // check deeply nested classes checkConst("class Fred {\n" @@ -2019,8 +2019,8 @@ private: " };\n" " int B::getB() { return b; }\n" "};"); - ASSERT_EQUALS("[test.cpp:11] -> [test.cpp:4]: (style) The function 'Fred::B::getB' can be const\n" - "[test.cpp:9] -> [test.cpp:7]: (style) The function 'Fred::B::A::getA' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:11]: (style) The function 'Fred::B::getB' can be const\n" + "[test.cpp:9]: (style) The function 'Fred::B::A::getA' can be const\n", errout.str()); checkConst("class Fred {\n" " class B {\n" @@ -2034,8 +2034,8 @@ private: "};\n" "int Fred::B::A::getA() { return a; }\n" "int Fred::B::getB() { return b; }\n"); - ASSERT_EQUALS("[test.cpp:12] -> [test.cpp:4]: (style) The function 'Fred::B::getB' can be const\n" - "[test.cpp:11] -> [test.cpp:7]: (style) The function 'Fred::B::A::getA' can be const\n", errout.str()); + ASSERT_EQUALS("[test.cpp:12]: (style) The function 'Fred::B::getB' can be const\n" + "[test.cpp:11]: (style) The function 'Fred::B::A::getA' can be const\n", errout.str()); } // operator< can often be const From cffe20a4408e10905162b6d50916a9b255712113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 9 Mar 2010 08:10:05 +0100 Subject: [PATCH 03/11] dmake: debug/release mode, more gcc warnings --- Makefile | 2 +- tools/dmake.cpp | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 50d1bbe2f..0667a610b 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CXXFLAGS=-Wall -Wextra -pedantic -g +CXXFLAGS=-Wall -Wextra -pedantic -Wfloat-equal -Wcast-qual -Wsign-conversion -Wlogical-op -g CXX=g++ BIN=${DESTDIR}/usr/bin diff --git a/tools/dmake.cpp b/tools/dmake.cpp index 99aca367b..394befe2f 100644 --- a/tools/dmake.cpp +++ b/tools/dmake.cpp @@ -94,8 +94,10 @@ static void getCppFiles(std::vector &files, const std::string &path } } -int main() +int main(int argc, char **argv) { + const bool release(argc >= 2 && std::string(argv[1]) == "--release"); + // Get files.. std::vector libfiles; getCppFiles(libfiles, "lib/"); @@ -138,8 +140,9 @@ int main() std::ofstream fout("Makefile"); - // more warnings.. -Wfloat-equal -Wcast-qual -Wsign-conversion -Wlogical-op - fout << "CXXFLAGS=-Wall -Wextra -pedantic -g\n"; + // Makefile settings.. + fout << "CXXFLAGS=-Wall -Wextra -pedantic -Wfloat-equal -Wcast-qual -Wsign-conversion -Wlogical-op "; + fout << (release ? "-O2 -DNDEBUG" : "-g") << "\n"; fout << "CXX=g++\n"; fout << "BIN=${DESTDIR}/usr/bin\n\n"; fout << "# For 'make man': sudo apt-get install xsltproc docbook-xsl docbook-xml\n"; From 2ba3f59fc8044f52badb88f9697385036566b6b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 9 Mar 2010 08:24:52 +0100 Subject: [PATCH 04/11] dmake, qmake: more sensitive compiler and runtime --- cli/cli.pro | 3 +-- tools/dmake.cpp | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/cli/cli.pro b/cli/cli.pro index b71023069..3e3da246d 100644 --- a/cli/cli.pro +++ b/cli/cli.pro @@ -26,9 +26,8 @@ win32 { } # Add more strict compiling flags for GCC -# These flags are used in original (not generated by QMake) makefiles contains(QMAKE_CXX, g++) { - QMAKE_CXXFLAGS_WARN_ON += -Wextra -pedantic + QMAKE_CXXFLAGS_WARN_ON += -Wextra -pedantic -Wfloat-equal -Wcast-qual -Wsign-conversion -Wlogical-op CONFIG(debug, debug|release) { # checked STL diff --git a/tools/dmake.cpp b/tools/dmake.cpp index 394befe2f..5ed404f17 100644 --- a/tools/dmake.cpp +++ b/tools/dmake.cpp @@ -142,7 +142,7 @@ int main(int argc, char **argv) // Makefile settings.. fout << "CXXFLAGS=-Wall -Wextra -pedantic -Wfloat-equal -Wcast-qual -Wsign-conversion -Wlogical-op "; - fout << (release ? "-O2 -DNDEBUG" : "-g") << "\n"; + fout << (release ? "-O2 -DNDEBUG" : "-g -D_GLIBCXX_DEBUG") << "\n"; fout << "CXX=g++\n"; fout << "BIN=${DESTDIR}/usr/bin\n\n"; fout << "# For 'make man': sudo apt-get install xsltproc docbook-xsl docbook-xml\n"; From 0158c7aa556f36b0ac269c3fb65806b2d01ed9b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 9 Mar 2010 08:30:18 +0100 Subject: [PATCH 05/11] Makefile: updated by latest dmake --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 0667a610b..7fcfd1c7b 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CXXFLAGS=-Wall -Wextra -pedantic -Wfloat-equal -Wcast-qual -Wsign-conversion -Wlogical-op -g +CXXFLAGS=-Wall -Wextra -pedantic -Wfloat-equal -Wcast-qual -Wsign-conversion -Wlogical-op -g -D_GLIBCXX_DEBUG CXX=g++ BIN=${DESTDIR}/usr/bin From ae8059dea9f109181640651d5f7f3f72cbcd8380 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 9 Mar 2010 10:41:36 +0100 Subject: [PATCH 06/11] createrelease: set release mode for Makefile --- createrelease | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/createrelease b/createrelease index 1436b3f26..a2fc7b4d9 100755 --- a/createrelease +++ b/createrelease @@ -10,14 +10,29 @@ # - win_installer/productInfo.wxs # Tag to use -tag=1.41 +tag=$1 # Name of release releasename=cppcheck-$tag # wget http://josefsson.org/git2cl/git2cl +echo Update Changelog.. ./git2cl > ChangeLog +echo Update Makefile.. +git tag -d $tag +g++ -o dmake tools/dmake.cpp lib/filelister.cpp +./dmake --release +git commit -a -m "Makefile: Set release mode" +git tag $tag + +echo Create archives.. git archive --format=tar --prefix=$releasename/ $tag | gzip > ~/$releasename.tar.gz git archive --format=tar --prefix=$releasename/ $tag | bzip2 > ~/$releasename.tar.bz2 git archive --format=zip -9 --prefix=$releasename/ $tag > ~/$releasename.zip + +echo Restoring repository.. +git tag -d $tag +git reset --hard HEAD^1 +git pull + From 3123de346cd959244315a687c7c8adc89ae46488 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 9 Mar 2010 10:56:31 +0100 Subject: [PATCH 07/11] checkbufferoverrun: Fixed signedness compiler warnings --- lib/checkbufferoverrun.cpp | 41 ++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index b0aaafbfa..92241ef1f 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -182,11 +182,8 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vectornext(); @@ -597,8 +594,8 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vectorstrAt(6))) { - size_t len = MathLib::toLongNumber(tok->strAt(6)); - if (len > static_cast(total_size)) + long len = MathLib::toLongNumber(tok->strAt(6)); + if (len < 0 || len > total_size) { bufferOverrun(tok); continue; @@ -610,8 +607,8 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vectorstrAt(4))) { - size_t len = MathLib::toLongNumber(tok->strAt(4)); - if (len > static_cast(total_size)) + long len = MathLib::toLongNumber(tok->strAt(4)); + if (len < 0 || len > total_size) { bufferOverrun(tok); continue; @@ -621,8 +618,8 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector 0 && Token::Match(tok, "strncat ( %varid% , %any% , %num% )", varid)) { - int n = MathLib::toLongNumber(tok->strAt(6)); - if (n >= total_size) + long n = MathLib::toLongNumber(tok->strAt(6)); + if (n < 0 || n >= total_size) strncatUsage(tok); } @@ -630,7 +627,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector 0 && Token::Match(tok, "strncpy|strncat ( %varid% , %any% , %num% ) ; strncat ( %varid% , %any% , %num% )", varid)) { - int n = MathLib::toLongNumber(tok->strAt(6)) + MathLib::toLongNumber(tok->strAt(15)); + long n = MathLib::toLongNumber(tok->strAt(6)) + MathLib::toLongNumber(tok->strAt(15)); if (n > total_size) strncatUsage(tok->tokAt(9)); } @@ -793,7 +790,7 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable() else if (tok->str() == "}") --indentlevel; - unsigned int size = 0; + long size = 0; std::string type; unsigned int varid = 0; int nextTok = 0; @@ -804,7 +801,7 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable() if (Token::Match(tok, "%type% *| %var% [ %num% ] [;=]")) { - unsigned int varpos = 1; + int varpos = 1; if (tok->next()->str() == "*") ++varpos; size = MathLib::toLongNumber(tok->strAt(varpos + 2)); @@ -841,7 +838,7 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable() // malloc() gets count of bytes and not count of // elements, so we should calculate count of elements // manually - unsigned int sizeOfType = _tokenizer->sizeOfType(declTok); + int sizeOfType = (int)_tokenizer->sizeOfType(declTok); if (sizeOfType > 0) size /= sizeOfType; } @@ -856,7 +853,7 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable() Token sizeTok(0); sizeTok.str(type); - int total_size = size * _tokenizer->sizeOfType(&sizeTok); + int total_size = size * (int)_tokenizer->sizeOfType(&sizeTok); if (total_size == 0) continue; @@ -903,9 +900,9 @@ void CheckBufferOverrun::checkStructVariable() const unsigned int varId = tok2->tokAt(ivar)->varId(); varname[1] = tok2->strAt(ivar); int arrsize = MathLib::toLongNumber(tok2->strAt(ivar + 2)); - int total_size = arrsize * _tokenizer->sizeOfType(tok2->tokAt(1)); + int total_size = arrsize * (int)_tokenizer->sizeOfType(tok2->tokAt(1)); if (tok2->tokAt(2)->str() == "*") - total_size = arrsize * _tokenizer->sizeOfType(tok2->tokAt(2)); + total_size = arrsize * (int)_tokenizer->sizeOfType(tok2->tokAt(2)); if (total_size == 0) continue; @@ -1005,7 +1002,7 @@ int CheckBufferOverrun::countSprintfLength(const std::string &input_string, cons std::string digits_string = ""; bool i_d_x_f_found = false; std::list::const_iterator paramIter = parameters.begin(); - unsigned int parameterLength = 0; + int parameterLength = 0; for (std::string::size_type i = 0; i < input_string.length(); ++i) { if (input_string[i] == '\\') @@ -1040,13 +1037,13 @@ int CheckBufferOverrun::countSprintfLength(const std::string &input_string, cons case 'd': i_d_x_f_found = true; if (paramIter != parameters.end() && *paramIter && (*paramIter)->str()[0] != '"') - parameterLength = (*paramIter)->str().length(); + parameterLength = (int)(*paramIter)->str().length(); handleNextParameter = true; break; case 's': if (paramIter != parameters.end() && *paramIter && (*paramIter)->str()[0] == '"') - parameterLength = Token::getStrLength(*paramIter); + parameterLength = (int)Token::getStrLength(*paramIter); handleNextParameter = true; break; @@ -1065,14 +1062,14 @@ int CheckBufferOverrun::countSprintfLength(const std::string &input_string, cons if (handleNextParameter) { - unsigned int tempDigits = std::abs(std::atoi(digits_string.c_str())); + int tempDigits = std::abs(MathLib::toLongNumber(digits_string)); if (i_d_x_f_found) tempDigits = std::max(static_cast(tempDigits), 1); if (digits_string.find('.') != std::string::npos) { const std::string endStr = digits_string.substr(digits_string.find('.') + 1); - unsigned int maxLen = std::max(std::abs(std::atoi(endStr.c_str())), 1); + int maxLen = std::max((int)std::abs(MathLib::toLongNumber(endStr)), 1); if (input_string[i] == 's') { From 0597026f1342afe540573c0abdb4cd7a4f26c3d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 9 Mar 2010 11:03:45 +0100 Subject: [PATCH 08/11] Revert "checkbufferoverrun: Fixed signedness compiler warnings" This reverts commit 3123de346cd959244315a687c7c8adc89ae46488. This commit caused failed tests --- lib/checkbufferoverrun.cpp | 41 ++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 92241ef1f..b0aaafbfa 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -182,8 +182,11 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vectornext(); @@ -594,8 +597,8 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vectorstrAt(6))) { - long len = MathLib::toLongNumber(tok->strAt(6)); - if (len < 0 || len > total_size) + size_t len = MathLib::toLongNumber(tok->strAt(6)); + if (len > static_cast(total_size)) { bufferOverrun(tok); continue; @@ -607,8 +610,8 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vectorstrAt(4))) { - long len = MathLib::toLongNumber(tok->strAt(4)); - if (len < 0 || len > total_size) + size_t len = MathLib::toLongNumber(tok->strAt(4)); + if (len > static_cast(total_size)) { bufferOverrun(tok); continue; @@ -618,8 +621,8 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector 0 && Token::Match(tok, "strncat ( %varid% , %any% , %num% )", varid)) { - long n = MathLib::toLongNumber(tok->strAt(6)); - if (n < 0 || n >= total_size) + int n = MathLib::toLongNumber(tok->strAt(6)); + if (n >= total_size) strncatUsage(tok); } @@ -627,7 +630,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector 0 && Token::Match(tok, "strncpy|strncat ( %varid% , %any% , %num% ) ; strncat ( %varid% , %any% , %num% )", varid)) { - long n = MathLib::toLongNumber(tok->strAt(6)) + MathLib::toLongNumber(tok->strAt(15)); + int n = MathLib::toLongNumber(tok->strAt(6)) + MathLib::toLongNumber(tok->strAt(15)); if (n > total_size) strncatUsage(tok->tokAt(9)); } @@ -790,7 +793,7 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable() else if (tok->str() == "}") --indentlevel; - long size = 0; + unsigned int size = 0; std::string type; unsigned int varid = 0; int nextTok = 0; @@ -801,7 +804,7 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable() if (Token::Match(tok, "%type% *| %var% [ %num% ] [;=]")) { - int varpos = 1; + unsigned int varpos = 1; if (tok->next()->str() == "*") ++varpos; size = MathLib::toLongNumber(tok->strAt(varpos + 2)); @@ -838,7 +841,7 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable() // malloc() gets count of bytes and not count of // elements, so we should calculate count of elements // manually - int sizeOfType = (int)_tokenizer->sizeOfType(declTok); + unsigned int sizeOfType = _tokenizer->sizeOfType(declTok); if (sizeOfType > 0) size /= sizeOfType; } @@ -853,7 +856,7 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable() Token sizeTok(0); sizeTok.str(type); - int total_size = size * (int)_tokenizer->sizeOfType(&sizeTok); + int total_size = size * _tokenizer->sizeOfType(&sizeTok); if (total_size == 0) continue; @@ -900,9 +903,9 @@ void CheckBufferOverrun::checkStructVariable() const unsigned int varId = tok2->tokAt(ivar)->varId(); varname[1] = tok2->strAt(ivar); int arrsize = MathLib::toLongNumber(tok2->strAt(ivar + 2)); - int total_size = arrsize * (int)_tokenizer->sizeOfType(tok2->tokAt(1)); + int total_size = arrsize * _tokenizer->sizeOfType(tok2->tokAt(1)); if (tok2->tokAt(2)->str() == "*") - total_size = arrsize * (int)_tokenizer->sizeOfType(tok2->tokAt(2)); + total_size = arrsize * _tokenizer->sizeOfType(tok2->tokAt(2)); if (total_size == 0) continue; @@ -1002,7 +1005,7 @@ int CheckBufferOverrun::countSprintfLength(const std::string &input_string, cons std::string digits_string = ""; bool i_d_x_f_found = false; std::list::const_iterator paramIter = parameters.begin(); - int parameterLength = 0; + unsigned int parameterLength = 0; for (std::string::size_type i = 0; i < input_string.length(); ++i) { if (input_string[i] == '\\') @@ -1037,13 +1040,13 @@ int CheckBufferOverrun::countSprintfLength(const std::string &input_string, cons case 'd': i_d_x_f_found = true; if (paramIter != parameters.end() && *paramIter && (*paramIter)->str()[0] != '"') - parameterLength = (int)(*paramIter)->str().length(); + parameterLength = (*paramIter)->str().length(); handleNextParameter = true; break; case 's': if (paramIter != parameters.end() && *paramIter && (*paramIter)->str()[0] == '"') - parameterLength = (int)Token::getStrLength(*paramIter); + parameterLength = Token::getStrLength(*paramIter); handleNextParameter = true; break; @@ -1062,14 +1065,14 @@ int CheckBufferOverrun::countSprintfLength(const std::string &input_string, cons if (handleNextParameter) { - int tempDigits = std::abs(MathLib::toLongNumber(digits_string)); + unsigned int tempDigits = std::abs(std::atoi(digits_string.c_str())); if (i_d_x_f_found) tempDigits = std::max(static_cast(tempDigits), 1); if (digits_string.find('.') != std::string::npos) { const std::string endStr = digits_string.substr(digits_string.find('.') + 1); - int maxLen = std::max((int)std::abs(MathLib::toLongNumber(endStr)), 1); + unsigned int maxLen = std::max(std::abs(std::atoi(endStr.c_str())), 1); if (input_string[i] == 's') { From 2c210b8ff91738c805059375996b4c885b95c51c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 9 Mar 2010 11:10:34 +0100 Subject: [PATCH 09/11] dmake,qmake: temporarily removed -Wsign-conversion --- Makefile | 2 +- cli/cli.pro | 2 +- tools/dmake.cpp | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 7fcfd1c7b..99963120d 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CXXFLAGS=-Wall -Wextra -pedantic -Wfloat-equal -Wcast-qual -Wsign-conversion -Wlogical-op -g -D_GLIBCXX_DEBUG +CXXFLAGS=-Wall -Wextra -pedantic -Wfloat-equal -Wcast-qual -Wlogical-op -g -D_GLIBCXX_DEBUG CXX=g++ BIN=${DESTDIR}/usr/bin diff --git a/cli/cli.pro b/cli/cli.pro index 3e3da246d..0f63308c1 100644 --- a/cli/cli.pro +++ b/cli/cli.pro @@ -27,7 +27,7 @@ win32 { # Add more strict compiling flags for GCC contains(QMAKE_CXX, g++) { - QMAKE_CXXFLAGS_WARN_ON += -Wextra -pedantic -Wfloat-equal -Wcast-qual -Wsign-conversion -Wlogical-op + QMAKE_CXXFLAGS_WARN_ON += -Wextra -pedantic -Wfloat-equal -Wcast-qual -Wlogical-op CONFIG(debug, debug|release) { # checked STL diff --git a/tools/dmake.cpp b/tools/dmake.cpp index 5ed404f17..66c3e2230 100644 --- a/tools/dmake.cpp +++ b/tools/dmake.cpp @@ -141,7 +141,8 @@ int main(int argc, char **argv) std::ofstream fout("Makefile"); // Makefile settings.. - fout << "CXXFLAGS=-Wall -Wextra -pedantic -Wfloat-equal -Wcast-qual -Wsign-conversion -Wlogical-op "; + // TODO: add more compiler warnings. For example -Wsign-conversion + fout << "CXXFLAGS=-Wall -Wextra -pedantic -Wfloat-equal -Wcast-qual -Wlogical-op "; fout << (release ? "-O2 -DNDEBUG" : "-g -D_GLIBCXX_DEBUG") << "\n"; fout << "CXX=g++\n"; fout << "BIN=${DESTDIR}/usr/bin\n\n"; From 5d68952bd2ce39456f14470c97f792cf420ff960 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 9 Mar 2010 12:04:22 +0100 Subject: [PATCH 10/11] checkbufferoverrun: Refactorings --- lib/checkbufferoverrun.cpp | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index b0aaafbfa..7359690ec 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -182,11 +182,8 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vectornext(); @@ -584,8 +581,8 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector 0 && Token::Match(tok, "strcpy|strcat ( %varid% , %str% )", varid)) || (varid == 0 && Token::Match(tok, ("strcpy|strcat ( " + varnames + " , %str% )").c_str()))) { - size_t len = Token::getStrLength(tok->tokAt(varc + 4)); - if (len >= static_cast(total_size)) + long len = Token::getStrLength(tok->tokAt(varc + 4)); + if (len < 0 || len >= total_size) { bufferOverrun(tok, varid > 0 ? "" : varnames.c_str()); continue; @@ -597,8 +594,8 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vectorstrAt(6))) { - size_t len = MathLib::toLongNumber(tok->strAt(6)); - if (len > static_cast(total_size)) + long len = MathLib::toLongNumber(tok->strAt(6)); + if (len < 0 || len > total_size) { bufferOverrun(tok); continue; @@ -610,8 +607,8 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vectorstrAt(4))) { - size_t len = MathLib::toLongNumber(tok->strAt(4)); - if (len > static_cast(total_size)) + long len = MathLib::toLongNumber(tok->strAt(4)); + if (len < 0 || len > total_size) { bufferOverrun(tok); continue; @@ -622,7 +619,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector 0 && Token::Match(tok, "strncat ( %varid% , %any% , %num% )", varid)) { int n = MathLib::toLongNumber(tok->strAt(6)); - if (n >= total_size) + if (n < 0 || n >= total_size) strncatUsage(tok); } From 04ff061bbb1c4d4d894196f9ebf52481d443a4ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 9 Mar 2010 12:41:40 +0100 Subject: [PATCH 11/11] astyle formatting --- lib/checkbufferoverrun.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 7359690ec..6d81d909c 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -183,7 +183,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vectornext();