From 04b38e5428c60ee76312d1f1a4a3cb2a348c4c8c Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Mon, 5 Apr 2010 20:57:54 +0300 Subject: [PATCH 1/2] astyle fix --- lib/checkother.cpp | 22 +++++++++++----------- lib/checkother.h | 2 +- lib/mathlib.cpp | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 792538eff..5d3ccc5ac 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2563,18 +2563,18 @@ void CheckOther::checkMathFunctions() { mathfunctionCallError(tok); } - // atan2 ( x , y): x and y can not be zero, because this is mathematically not defined + // atan2 ( x , y): x and y can not be zero, because this is mathematically not defined else if (Token::Match(tok, "atan2 ( %num% , %num% )") && MathLib::isNullValue(tok->tokAt(2)->str()) && - MathLib::isNullValue(tok->tokAt(4)->str())) + MathLib::isNullValue(tok->tokAt(4)->str())) { - mathfunctionCallError(tok,2); + mathfunctionCallError(tok, 2); } - // fmod ( x , y) If y is zero, then either a range error will occur or the function will return zero (implementation-defined). + // fmod ( x , y) If y is zero, then either a range error will occur or the function will return zero (implementation-defined). else if (Token::Match(tok, "fmod ( %num% , %num% )") && - MathLib::isNullValue(tok->tokAt(4)->str())) + MathLib::isNullValue(tok->tokAt(4)->str())) { - mathfunctionCallError(tok,2); + mathfunctionCallError(tok, 2); } } @@ -2728,11 +2728,11 @@ void CheckOther::mathfunctionCallError(const Token *tok, const unsigned int numP { if (tok) { - if(numParam == 1) - reportError(tok, Severity::error, "wrongmathcall", "Passing value " + tok->tokAt(2)->str() + " to " + tok->str() + "() leads to undefined result"); - else if (numParam == 2) - reportError(tok, Severity::error, "wrongmathcall", "Passing value " + tok->tokAt(2)->str() + " and " + tok->tokAt(4)->str() + " to " + tok->str() + "() leads to undefined result"); - } + if (numParam == 1) + reportError(tok, Severity::error, "wrongmathcall", "Passing value " + tok->tokAt(2)->str() + " to " + tok->str() + "() leads to undefined result"); + else if (numParam == 2) + reportError(tok, Severity::error, "wrongmathcall", "Passing value " + tok->tokAt(2)->str() + " and " + tok->tokAt(4)->str() + " to " + tok->str() + "() leads to undefined result"); + } else reportError(tok, Severity::error, "wrongmathcall", "Passing value " " to " "() leads to undefined result"); } diff --git a/lib/checkother.h b/lib/checkother.h index c763735f9..7d4aa5c37 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -189,7 +189,7 @@ public: void uninitdataError(const Token *tok, const std::string &varname); void uninitvarError(const Token *tok, const std::string &varname); void zerodivError(const Token *tok); - void mathfunctionCallError(const Token *tok, const unsigned int numParam=1); + void mathfunctionCallError(const Token *tok, const unsigned int numParam = 1); void postIncrementError(const Token *tok, const std::string &var_name, const bool isIncrement); void getErrorMessages() diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index 14287b7ce..368bddbb9 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -300,7 +300,7 @@ bool MathLib::isGreater(const std::string &first, const std::string &second) bool MathLib::isNullValue(const std::string &str) { return (str == "-0" || str == "-0.0" - || str == "0" + || str == "0" || str == "-0." || str == "-0E-00" || str == "-0E+00" || str == "+0E+00" || str == "+0E-00" || str == "+0" From 6327ed55a2969a238248419bd2bc8a1a3aa6c7eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 5 Apr 2010 20:02:28 +0200 Subject: [PATCH 2/2] Unit Testing: Buffer overruns when using memchr/memset/memcpy/etc --- test/testbufferoverrun.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 82786c488..372821358 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -130,6 +130,8 @@ private: TEST_CASE(strncat1); TEST_CASE(strncat2); + TEST_CASE(memfunc); // memchr/memset/memcpy + TEST_CASE(cin1); TEST_CASE(varid1); @@ -1432,6 +1434,42 @@ private: + // memchr/memset/memcpy/etc + void memfunc() + { + check("void f()\n" + "{\n" + " char str[5];\n" + " memset(str, 0, 10);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (error) Buffer access out-of-bounds\n", errout.str()); + + check("void f()\n" + "{\n" + " char a[5], b[50];\n" + " memcpy(a, b, 10);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (error) Buffer access out-of-bounds\n", errout.str()); + + check("void f()\n" + "{\n" + " char a[5], b[50];\n" + " memmove(a, b, 10);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (error) Buffer access out-of-bounds\n", errout.str()); + + // When this TODO assertion works, ticket #909 can probably be closed + check("void f()\n" + "{\n" + " char a[5], b[50];\n" + " memchr(a, b, 10);\n" + "}\n"); + TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Buffer access out-of-bounds\n", errout.str()); + ASSERT_EQUALS("", errout.str()); + } + + + void cin1() { check("#include \n"