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/>.
*/
//---------------------------------------------------------------------------
# ifndef CheckObsoleteFunctionsH
# define CheckObsoleteFunctionsH
//---------------------------------------------------------------------------
# include "check.h"
2010-08-15 21:26:13 +02:00
# include <string>
# include <list>
2010-08-14 20:13:46 +02:00
/// @addtogroup Checks
/// @{
/**
* @ brief Using obsolete functions that are always insecure to use .
*/
class CheckObsoleteFunctions : public Check
{
public :
/** This constructor is used when registering the CheckObsoleteFunctions */
2011-02-02 10:29:10 +01:00
CheckObsoleteFunctions ( ) : Check ( myName ( ) )
2010-08-16 18:51:25 +02:00
{
initObsoleteFunctions ( ) ;
}
2010-08-14 20:13:46 +02:00
/** This constructor is used when running checks. */
CheckObsoleteFunctions ( const Tokenizer * tokenizer , const Settings * settings , ErrorLogger * errorLogger )
2011-02-02 10:29:10 +01:00
: Check ( myName ( ) , tokenizer , settings , errorLogger )
2010-08-16 18:51:25 +02:00
{
initObsoleteFunctions ( ) ;
}
2010-08-14 20:13:46 +02:00
void runSimplifiedChecks ( const Tokenizer * tokenizer , const Settings * settings , ErrorLogger * errorLogger )
{
CheckObsoleteFunctions checkObsoleteFunctions ( tokenizer , settings , errorLogger ) ;
checkObsoleteFunctions . obsoleteFunctions ( ) ;
}
/** Check for obsolete functions */
void obsoleteFunctions ( ) ;
private :
2010-08-16 18:51:25 +02:00
/* function name / error message */
2011-08-03 16:10:43 +02:00
std : : map < std : : string , std : : string > _obsoleteStandardFunctions ;
std : : map < std : : string , std : : string > _obsoletePosixFunctions ;
2010-08-14 20:13:46 +02:00
2010-08-15 21:26:13 +02:00
/** init obsolete functions list ' */
2010-08-16 18:51:25 +02:00
void initObsoleteFunctions ( )
{
2011-08-03 16:10:43 +02:00
_obsoletePosixFunctions [ " bsd_signal " ] = " Found obsolete function 'bsd_signal'. It is recommended that new applications use the 'sigaction' function " ;
2010-08-14 20:13:46 +02:00
2011-08-03 16:10:43 +02:00
_obsoletePosixFunctions [ " gethostbyaddr " ] = " Found obsolete function 'gethostbyaddr'. It is recommended that new applications use the 'getnameinfo' function " ;
_obsoletePosixFunctions [ " gethostbyname " ] = " Found obsolete function 'gethostbyname'. It is recommended that new applications use the 'getaddrinfo' function " ;
2010-08-14 20:13:46 +02:00
2011-08-03 16:10:43 +02:00
_obsoletePosixFunctions [ " usleep " ] = " Found obsolete function 'usleep'. It is recommended that new applications use the 'nanosleep' or 'setitimer' function \n "
2011-08-03 16:11:09 +02:00
" Found obsolete function 'usleep'. POSIX.1-2001 declares usleep() function obsolete and POSIX.1-2008 removes it. It is recommended that new applications use the 'nanosleep' or 'setitimer' function. " ;
2010-08-14 20:13:46 +02:00
2011-08-03 16:10:43 +02:00
_obsoletePosixFunctions [ " bcmp " ] = " Found obsolete function 'bcmp'. It is recommended that new applications use the 'memcmp' function " ;
_obsoletePosixFunctions [ " bcopy " ] = " Found obsolete function 'bcopy'. It is recommended that new applications use the 'memmove' or 'memcpy' functions " ;
_obsoletePosixFunctions [ " bzero " ] = " Found obsolete function 'bzero'. It is recommended that new applications use the 'memset' function " ;
2010-08-16 18:51:25 +02:00
2011-08-03 16:10:43 +02:00
_obsoletePosixFunctions [ " ecvt " ] = " Found obsolete function 'ecvt'. It is recommended that new applications use the 'sprintf' function " ;
_obsoletePosixFunctions [ " fcvt " ] = " Found obsolete function 'fcvt'. It is recommended that new applications use the 'sprintf' function " ;
_obsoletePosixFunctions [ " gcvt " ] = " Found obsolete function 'gcvt'. It is recommended that new applications use the 'sprintf' function " ;
2010-08-14 20:13:46 +02:00
2011-08-03 16:10:43 +02:00
_obsoletePosixFunctions [ " ftime " ] = " Found obsolete function 'ftime'. \n "
2011-08-03 16:11:09 +02:00
" It is recommended that new applications use time(), gettimeofday(), or clock_gettime() instead. "
" For high-resolution timing on Windows, QueryPerformanceCounter() and QueryPerformanceFrequency may be used. " ;
2010-08-14 20:13:46 +02:00
2011-08-03 16:10:43 +02:00
_obsoletePosixFunctions [ " getcontext " ] = " Found obsolete function 'getcontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads " ;
_obsoletePosixFunctions [ " makecontext " ] = " Found obsolete function 'makecontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads " ;
_obsoletePosixFunctions [ " swapcontext " ] = " Found obsolete function 'swapcontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads " ;
2010-08-14 20:13:46 +02:00
2011-08-03 16:10:43 +02:00
_obsoletePosixFunctions [ " getwd " ] = " Found obsolete function 'getwd'. It is recommended that new applications use the 'getcwd' function " ;
2010-08-14 20:13:46 +02:00
2011-08-03 16:10:43 +02:00
// See #2334 (using the Qt Model/View function 'index')
_obsoletePosixFunctions [ " index " ] = " Found obsolete function 'index'. It is recommended to use the function 'strchr' instead " ;
2010-12-20 18:13:26 +01:00
2011-08-03 16:10:43 +02:00
_obsoletePosixFunctions [ " rindex " ] = " Found obsolete function 'rindex'. It is recommended to use the function 'strrchr' instead " ;
2010-08-14 20:13:46 +02:00
2011-08-03 16:10:43 +02:00
_obsoletePosixFunctions [ " pthread_attr_getstackaddr " ] = " Found obsolete function 'pthread_attr_getstackaddr'.It is recommended that new applications use the 'pthread_attr_getstack' function " ;
_obsoletePosixFunctions [ " pthread_attr_setstackaddr " ] = " Found obsolete function 'pthread_attr_setstackaddr'.It is recommended that new applications use the 'pthread_attr_setstack' function " ;
2010-08-14 20:13:46 +02:00
2011-08-03 16:10:43 +02:00
_obsoletePosixFunctions [ " scalbln " ] = " Found obsolete function 'scalb'.It is recommended to use either 'scalbln', 'scalblnf' or 'scalblnl' instead of this function " ;
2010-08-15 21:25:14 +02:00
2011-08-03 16:10:43 +02:00
_obsoletePosixFunctions [ " ualarm " ] = " Found obsolete function 'ualarm'.It is recommended to use either 'timer_create', 'timer_delete', 'timer_getoverrun', 'timer_gettime', or 'timer_settime' instead of this function " ;
2010-08-15 21:25:14 +02:00
2011-08-03 16:10:43 +02:00
_obsoletePosixFunctions [ " vfork " ] = " Found obsolete function 'vfork'. It is recommended to use the function 'fork' instead " ;
2010-08-15 21:25:14 +02:00
2011-08-03 16:10:43 +02:00
_obsoletePosixFunctions [ " wcswcs " ] = " Found obsolete function 'wcswcs'. It is recommended to use the function 'wcsstr' instead " ;
2010-08-31 19:48:04 +02:00
2011-08-03 16:10:43 +02:00
_obsoleteStandardFunctions [ " gets " ] = " Found obsolete function 'gets'. It is recommended to use the function 'fgets' instead \n "
2011-08-03 16:11:09 +02:00
" Found obsolete function 'gets'. With gets you'll get buffer overruns if the input data too big for the buffer. It is recommended to use the function 'fgets' instead. " ;
2010-08-15 21:25:14 +02:00
2010-08-15 21:26:13 +02:00
}
2010-08-14 20:13:46 +02:00
2010-12-29 12:43:29 +01:00
void getErrorMessages ( ErrorLogger * errorLogger , const Settings * settings )
2010-08-14 20:13:46 +02:00
{
2010-12-29 12:43:29 +01:00
CheckObsoleteFunctions c ( 0 , settings , errorLogger ) ;
2011-08-03 16:10:43 +02:00
std : : map < std : : string , std : : string > : : const_iterator it ( _obsoletePosixFunctions . begin ( ) ) , itend ( _obsoletePosixFunctions . end ( ) ) ;
2010-08-16 18:51:25 +02:00
for ( ; it ! = itend ; + + it )
{
2010-12-29 12:43:29 +01:00
c . reportError ( 0 , Severity : : style , " obsoleteFunctions " + it - > first , it - > second ) ;
2010-08-15 21:26:13 +02:00
}
2010-08-14 20:13:46 +02:00
}
2011-02-02 10:29:10 +01:00
std : : string myName ( ) const
2010-08-14 20:13:46 +02:00
{
return " Obsolete functions " ;
}
std : : string classInfo ( ) const
{
2010-08-15 21:26:13 +02:00
std : : string info = " Warn if any of these obsolete functions are used: \n " ;
2011-08-03 16:10:43 +02:00
std : : map < std : : string , std : : string > : : const_iterator it ( _obsoletePosixFunctions . begin ( ) ) , itend ( _obsoletePosixFunctions . end ( ) ) ;
2010-08-16 18:51:25 +02:00
for ( ; it ! = itend ; + + it )
{
2010-08-15 21:26:13 +02:00
info + = " * " + it - > first + " \n " ;
}
return info ;
2010-08-14 20:13:46 +02:00
}
} ;
/// @}
//---------------------------------------------------------------------------
# endif