2010-08-14 20:13:46 +02:00
/*
* Cppcheck - A tool for static C / C + + code analysis
2011-01-09 20:33:36 +01:00
* Copyright ( C ) 2007 - 2011 Daniel Marjamäki and Cppcheck team .
2010-08-14 20:13:46 +02: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 "tokenize.h"
# include "checkobsoletefunctions.h"
# include "testsuite.h"
# include <sstream>
extern std : : ostringstream errout ;
class TestObsoleteFunctions : public TestFixture
{
public :
TestObsoleteFunctions ( ) : TestFixture ( " TestObsoleteFunctions " )
{ }
private :
2010-08-29 12:01:32 +02:00
void run ( )
{
TEST_CASE ( testbsd_signal ) ;
TEST_CASE ( testgethostbyname ) ;
TEST_CASE ( testgethostbyaddr ) ;
TEST_CASE ( testusleep ) ;
TEST_CASE ( testindex ) ;
2010-12-20 18:13:26 +01:00
TEST_CASE ( test_qt_index ) ; // FP when using the Qt function 'index'?
2010-08-29 12:01:32 +02:00
TEST_CASE ( testrindex ) ;
// no false positives for variables
2010-08-31 18:58:01 +02:00
TEST_CASE ( testvar ) ;
2010-08-31 19:48:04 +02:00
2010-08-31 18:58:01 +02:00
// dangerous function
TEST_CASE ( testgets ) ;
2011-09-24 10:54:58 +02:00
// declared function ticket #3121
TEST_CASE ( test_declared_function ) ;
2010-08-29 12:01:32 +02:00
}
2010-08-14 20:13:46 +02:00
void check ( const char code [ ] )
{
2010-12-01 18:00:55 +01:00
// Clear the error buffer..
errout . str ( " " ) ;
Settings settings ;
2011-08-07 09:28:08 +02:00
settings . addEnabled ( " style " ) ;
2011-08-14 09:45:27 +02:00
settings . posix = true ;
2010-12-01 18:00:55 +01:00
2010-08-14 20:13:46 +02:00
// Tokenize..
2010-12-01 18:00:55 +01:00
Tokenizer tokenizer ( & settings , this ) ;
2010-08-14 20:13:46 +02:00
std : : istringstream istr ( code ) ;
tokenizer . tokenize ( istr , " test.cpp " ) ;
tokenizer . simplifyTokenList ( ) ;
// Assign variable ids
tokenizer . setVarId ( ) ;
// Fill function list
tokenizer . fillFunctionList ( ) ;
// Check for obsolete functions..
CheckObsoleteFunctions checkObsoleteFunctions ( & tokenizer , & settings , this ) ;
checkObsoleteFunctions . obsoleteFunctions ( ) ;
}
void testbsd_signal ( )
{
check ( " void f() \n "
" { \n "
" bsd_signal(SIGABRT, SIG_IGN); \n "
" } \n " ) ;
2011-08-03 16:10:43 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (information) Found obsolete function 'bsd_signal'. It is recommended that new applications use the 'sigaction' function \n " , errout . str ( ) ) ;
2010-08-14 20:13:46 +02:00
check ( " int f() \n "
" { \n "
" int bsd_signal(0); \n "
" return bsd_signal; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
void testgethostbyname ( )
{
check ( " void f() \n "
" { \n "
" struct hostent *hp; \n "
" if(!hp = gethostbyname('127.0.0.1')) { \n "
" exit(1); \n "
" } \n "
" } \n " ) ;
2011-08-03 16:10:43 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (information) Found obsolete function 'gethostbyname'. It is recommended that new applications use the 'getaddrinfo' function \n " , errout . str ( ) ) ;
2010-08-14 20:13:46 +02:00
}
void testgethostbyaddr ( )
{
check ( " void f() \n "
" { \n "
" long addr; \n "
" addr = inet_addr('127.0.0.1'); \n "
" if(!hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET)) { \n "
" exit(1); \n "
" } \n "
" } \n " ) ;
2011-08-03 16:10:43 +02:00
ASSERT_EQUALS ( " [test.cpp:5]: (information) Found obsolete function 'gethostbyaddr'. It is recommended that new applications use the 'getnameinfo' function \n " , errout . str ( ) ) ;
2010-08-14 20:13:46 +02:00
}
void testusleep ( )
{
check ( " void f() \n "
" { \n "
" usleep( 1000 ); \n "
" } \n " ) ;
2011-08-03 16:10:43 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (information) Found obsolete function 'usleep'. It is recommended that new applications use the 'nanosleep' or 'setitimer' function \n " , errout . str ( ) ) ;
2010-08-14 20:13:46 +02:00
}
void testindex ( )
{
check ( " namespace n1 { \n "
" int index(){}; \n "
" } \n "
" int main() \n "
" { \n "
" n1::index(); \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " std::size_t f() \n "
" { \n "
" std::size_t index(0); \n "
" index++; \n "
" return index; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int f() \n "
" { \n "
" return this->index(); \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() \n "
" { \n "
" int index( 0 ); \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " const char f() \n "
" { \n "
" const char var[6] = 'index'; \n "
" const char i = index(var, 0); \n "
" return i; \n "
" } \n " ) ;
2011-08-03 16:10:43 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (information) Found obsolete function 'index'. It is recommended to use the function 'strchr' instead \n " ,
errout . str ( ) ) ;
2010-12-20 18:13:26 +01:00
}
void test_qt_index ( )
{
check ( " void TDataModel::forceRowRefresh(int row) { \n "
" emit dataChanged(index(row, 0), index(row, columnCount() - 1)); \n "
" } \n " ) ;
2011-08-03 16:10:43 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (information) Found obsolete function 'index'. It is recommended to use the function 'strchr' instead \n " , errout . str ( ) ) ;
2010-08-14 20:13:46 +02:00
}
void testrindex ( )
{
check ( " void f() \n "
" { \n "
" int rindex( 0 ); \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() \n "
" { \n "
" const char var[7] = 'rindex'; \n "
" print(rindex(var, 0)); \n "
" } \n " ) ;
2011-08-03 16:10:43 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (information) Found obsolete function 'rindex'. It is recommended to use the function 'strrchr' instead \n " , errout . str ( ) ) ;
2010-08-14 20:13:46 +02:00
}
2010-08-31 18:58:01 +02:00
void testvar ( )
2010-08-29 12:01:32 +02:00
{
check ( " class Fred { \n "
" public: \n "
" Fred() : index(0) { } \n "
" int index; \n "
" }; \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-08-14 20:13:46 +02:00
2010-08-31 18:58:01 +02:00
void testgets ( )
{
check ( " void f() \n "
" { \n "
" char *x = gets(); \n "
" } \n " ) ;
2011-08-03 16:10:43 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (information) Found obsolete function 'gets'. It is recommended to use the function 'fgets' instead \n " , errout . str ( ) ) ;
2010-08-31 18:58:01 +02:00
}
2011-09-24 10:54:58 +02:00
// ticket #3121
void test_declared_function ( )
{
check ( " ftime ( int a ) \n "
" { \n "
" return a; \n "
" } \n "
" int main () \n "
" { \n "
" int b ; b = ftime ( 1 ) ; \n "
" return 0 ; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-08-14 20:13:46 +02:00
} ;
REGISTER_TEST ( TestObsoleteFunctions )