2009-02-10 20:40:21 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
|
|
|
* Copyright (C) 2007-2009 Daniel Marjamäki, Reijo Tomperi, Nicolas Le Cam,
|
2009-03-01 20:52:33 +01:00
|
|
|
* Leandro Penz, Kimmo Varis, Vesa Pikki
|
2009-02-10 20:40:21 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "../src/tokenize.h"
|
|
|
|
#include "../src/checkstl.h"
|
|
|
|
#include "testsuite.h"
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
extern std::ostringstream errout;
|
|
|
|
|
|
|
|
class TestStl : public TestFixture
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TestStl() : TestFixture("TestStl")
|
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void run()
|
|
|
|
{
|
2009-03-18 22:40:38 +01:00
|
|
|
/*
|
2009-02-10 20:40:21 +01:00
|
|
|
TEST_CASE(iterator1);
|
2009-02-10 21:01:39 +01:00
|
|
|
TEST_CASE(iterator2);
|
2009-02-10 20:56:00 +01:00
|
|
|
TEST_CASE(STLSize);
|
|
|
|
TEST_CASE(STLSizeNoErr);
|
2009-02-11 06:08:29 +01:00
|
|
|
TEST_CASE(erase);
|
|
|
|
TEST_CASE(eraseBreak);
|
2009-02-11 17:20:32 +01:00
|
|
|
TEST_CASE(eraseReturn);
|
|
|
|
TEST_CASE(eraseGoto);
|
2009-02-11 06:08:29 +01:00
|
|
|
TEST_CASE(eraseAssign);
|
2009-02-18 20:57:43 +01:00
|
|
|
|
|
|
|
TEST_CASE(pushback1);
|
2009-03-05 22:22:00 +01:00
|
|
|
TEST_CASE(invalidcode);
|
2009-03-18 22:40:38 +01:00
|
|
|
*/
|
2009-02-10 20:40:21 +01:00
|
|
|
}
|
|
|
|
|
2009-03-18 22:40:38 +01:00
|
|
|
/*
|
|
|
|
void check(const char code[])
|
|
|
|
{
|
|
|
|
// Tokenize..
|
|
|
|
Tokenizer tokenizer;
|
|
|
|
std::istringstream istr(code);
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
|
|
|
|
// Clear the error buffer..
|
|
|
|
errout.str("");
|
|
|
|
|
|
|
|
// Check char variable usage..
|
|
|
|
CheckStl checkStl(&tokenizer, this);
|
|
|
|
checkStl.iterators();
|
|
|
|
checkStl.stlOutOfBounds();
|
|
|
|
}
|
2009-02-10 20:56:00 +01:00
|
|
|
|
|
|
|
|
2009-03-18 22:40:38 +01:00
|
|
|
void iterator1()
|
2009-02-10 20:56:00 +01:00
|
|
|
{
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
2009-03-18 22:40:38 +01:00
|
|
|
" for (it = foo.begin(); it != bar.end(); ++it)\n"
|
|
|
|
" { }\n"
|
2009-02-10 20:56:00 +01:00
|
|
|
"}\n");
|
2009-03-18 22:40:38 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Same iterator is used with both foo and bar\n", errout.str());
|
2009-02-10 20:56:00 +01:00
|
|
|
}
|
|
|
|
|
2009-03-18 22:40:38 +01:00
|
|
|
void iterator2()
|
2009-02-10 20:56:00 +01:00
|
|
|
{
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
2009-03-18 22:40:38 +01:00
|
|
|
" it = foo.begin();\n"
|
|
|
|
" while (it != bar.end())\n"
|
2009-02-10 20:56:00 +01:00
|
|
|
" {\n"
|
2009-03-18 22:40:38 +01:00
|
|
|
" ++it;\n"
|
2009-02-10 20:56:00 +01:00
|
|
|
" }\n"
|
|
|
|
"}\n");
|
2009-03-18 22:40:38 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Same iterator is used with both foo and bar\n", errout.str());
|
2009-02-10 20:56:00 +01:00
|
|
|
}
|
2009-03-17 18:54:50 +01:00
|
|
|
|
2009-03-18 22:40:38 +01:00
|
|
|
|
|
|
|
void STLSize()
|
2009-03-17 18:54:50 +01:00
|
|
|
{
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" std::vector<int> foo;\n"
|
|
|
|
" for (unsigned int ii = 0; ii <= foo.size(); ++ii)\n"
|
|
|
|
" {\n"
|
2009-03-18 22:40:38 +01:00
|
|
|
" foo[ii] = 0;\n"
|
2009-03-17 18:54:50 +01:00
|
|
|
" }\n"
|
|
|
|
"}\n");
|
2009-03-18 22:40:38 +01:00
|
|
|
ASSERT_EQUALS(std::string("[test.cpp:6]: (error) When ii == size(), foo [ ii ] is out of bounds\n"), errout.str());
|
2009-03-17 18:54:50 +01:00
|
|
|
}
|
2009-02-11 06:08:29 +01:00
|
|
|
|
2009-03-18 22:40:38 +01:00
|
|
|
void STLSizeNoErr()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" std::vector<int> foo;\n"
|
|
|
|
" for (unsigned int ii = 0; ii < foo.size(); ++ii)\n"
|
|
|
|
" {\n"
|
|
|
|
" foo[ii] = 0;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS(std::string(""), errout.str());
|
|
|
|
}
|
2009-02-11 06:08:29 +01:00
|
|
|
|
2009-03-18 22:40:38 +01:00
|
|
|
{
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" std::vector<int> foo;\n"
|
|
|
|
" for (unsigned int ii = 0; ii <= foo.size(); ++ii)\n"
|
|
|
|
" {\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS(std::string(""), errout.str());
|
|
|
|
}
|
2009-02-11 06:08:29 +01:00
|
|
|
|
2009-03-18 22:40:38 +01:00
|
|
|
{
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" std::vector<int> foo;\n"
|
|
|
|
" for (unsigned int ii = 0; ii <= foo.size(); ++ii)\n"
|
|
|
|
" {\n"
|
|
|
|
" if (ii == foo.size())\n"
|
|
|
|
" {\n"
|
|
|
|
" }\n"
|
|
|
|
" else\n"
|
|
|
|
" {\n"
|
|
|
|
" foo[ii] = 0;\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n");
|
|
|
|
// TODO ASSERT_EQUALS(std::string(""), errout.str());
|
|
|
|
}
|
|
|
|
}
|
2009-02-11 06:08:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-03-18 22:40:38 +01:00
|
|
|
void checkErase(const char code[])
|
|
|
|
{
|
|
|
|
// Tokenize..
|
|
|
|
Tokenizer tokenizer;
|
|
|
|
std::istringstream istr(code);
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
2009-02-11 06:08:29 +01:00
|
|
|
|
2009-03-18 22:40:38 +01:00
|
|
|
// Clear the error buffer..
|
|
|
|
errout.str("");
|
2009-02-11 06:08:29 +01:00
|
|
|
|
2009-03-18 22:40:38 +01:00
|
|
|
// Check char variable usage..
|
|
|
|
CheckStl checkStl(&tokenizer, this);
|
|
|
|
checkStl.erase();
|
|
|
|
}
|
2009-02-11 06:08:29 +01:00
|
|
|
|
2009-02-11 17:20:32 +01:00
|
|
|
|
2009-03-18 22:40:38 +01:00
|
|
|
void erase()
|
|
|
|
{
|
|
|
|
checkErase("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" for (it = foo.begin(); it != foo.end(); ++it)\n"
|
|
|
|
" {\n"
|
|
|
|
" foo.erase(it);\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Dangerous usage of erase\n", errout.str());
|
|
|
|
}
|
2009-02-11 17:20:32 +01:00
|
|
|
|
2009-03-18 22:40:38 +01:00
|
|
|
void eraseBreak()
|
|
|
|
{
|
|
|
|
checkErase("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" for (it = foo.begin(); it != foo.end(); ++it)\n"
|
|
|
|
" {\n"
|
|
|
|
" foo.erase(it);\n"
|
|
|
|
" break;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void eraseReturn()
|
|
|
|
{
|
|
|
|
checkErase("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" for (it = foo.begin(); it != foo.end(); ++it)\n"
|
|
|
|
" {\n"
|
|
|
|
" foo.erase(it);\n"
|
|
|
|
" return;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void eraseGoto()
|
|
|
|
{
|
|
|
|
checkErase("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" for (it = foo.begin(); it != foo.end(); ++it)\n"
|
|
|
|
" {\n"
|
|
|
|
" foo.erase(it);\n"
|
|
|
|
" goto abc;\n"
|
|
|
|
" }\n"
|
|
|
|
"bar:\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
2009-02-11 06:08:29 +01:00
|
|
|
|
2009-03-18 22:40:38 +01:00
|
|
|
void eraseAssign()
|
|
|
|
{
|
|
|
|
checkErase("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" for (it = foo.begin(); it != foo.end(); ++it)\n"
|
|
|
|
" {\n"
|
|
|
|
" foo.erase(it);\n"
|
|
|
|
" it = foo.begin();\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
2009-02-11 06:08:29 +01:00
|
|
|
|
|
|
|
|
2009-02-18 20:57:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-03-18 22:40:38 +01:00
|
|
|
void checkPushback(const char code[])
|
|
|
|
{
|
|
|
|
// Tokenize..
|
|
|
|
Tokenizer tokenizer;
|
|
|
|
std::istringstream istr(code);
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
2009-02-18 20:57:43 +01:00
|
|
|
|
2009-03-18 22:40:38 +01:00
|
|
|
// Clear the error buffer..
|
|
|
|
errout.str("");
|
2009-02-18 20:57:43 +01:00
|
|
|
|
2009-03-18 22:40:38 +01:00
|
|
|
// Check char variable usage..
|
|
|
|
CheckStl checkStl(&tokenizer, this);
|
|
|
|
checkStl.pushback();
|
|
|
|
}
|
2009-02-18 20:57:43 +01:00
|
|
|
|
|
|
|
|
2009-03-18 22:40:38 +01:00
|
|
|
void pushback1()
|
|
|
|
{
|
|
|
|
checkPushback("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" std::vector<int>::const_iterator it = foo.begin();\n"
|
|
|
|
" foo.push_back(123);\n"
|
|
|
|
" *it;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) After push_back or push_front, the iterator 'it' may be invalid\n", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void invalidcode()
|
|
|
|
{
|
|
|
|
check("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" for ( \n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
*/
|
2009-02-10 20:40:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_TEST(TestStl)
|