Added a new check for finding non-reentrant functions

This commit is contained in:
Ettl Martin 2011-07-28 23:29:16 +02:00
parent d2f4b8e3de
commit 35bb5dffa4
5 changed files with 280 additions and 0 deletions

View File

@ -52,6 +52,7 @@ LIBOBJ = lib/check64bit.o \
lib/checkclass.o \
lib/checkexceptionsafety.o \
lib/checkmemoryleak.o \
lib/checknonreentrantfunctions.o \
lib/checknullpointer.o \
lib/checkobsoletefunctions.o \
lib/checkother.o \
@ -94,6 +95,7 @@ TESTOBJ = test/options.o \
test/testincompletestatement.o \
test/testmathlib.o \
test/testmemleak.o \
test/testnonreentrantfunctions.o \
test/testnullpointer.o \
test/testobsoletefunctions.o \
test/testoptions.o \
@ -183,6 +185,9 @@ lib/checkexceptionsafety.o: lib/checkexceptionsafety.cpp lib/checkexceptionsafet
lib/checkmemoryleak.o: lib/checkmemoryleak.cpp lib/checkmemoryleak.h lib/check.h lib/token.h lib/tokenize.h lib/settings.h lib/errorlogger.h lib/symboldatabase.h lib/mathlib.h lib/executionpath.h lib/checkuninitvar.h
$(CXX) $(CPPFLAGS) $(CXXFLAGS) ${INCLUDE_FOR_LIB} -c -o lib/checkmemoryleak.o lib/checkmemoryleak.cpp
lib/checknonreentrantfunctions.o: lib/checknonreentrantfunctions.cpp lib/checknonreentrantfunctions.h lib/check.h lib/token.h lib/tokenize.h lib/settings.h lib/errorlogger.h
$(CXX) $(CPPFLAGS) $(CXXFLAGS) ${INCLUDE_FOR_LIB} -c -o lib/checknonreentrantfunctions.o lib/checknonreentrantfunctions.cpp
lib/checknullpointer.o: lib/checknullpointer.cpp lib/checknullpointer.h lib/check.h lib/token.h lib/tokenize.h lib/settings.h lib/errorlogger.h lib/executionpath.h lib/mathlib.h lib/symboldatabase.h
$(CXX) $(CPPFLAGS) $(CXXFLAGS) ${INCLUDE_FOR_LIB} -c -o lib/checknullpointer.o lib/checknullpointer.cpp
@ -303,6 +308,9 @@ test/testmathlib.o: test/testmathlib.cpp lib/mathlib.h test/testsuite.h lib/erro
test/testmemleak.o: test/testmemleak.cpp lib/tokenize.h lib/checkmemoryleak.h lib/check.h lib/token.h lib/settings.h lib/errorlogger.h lib/symboldatabase.h lib/mathlib.h test/testsuite.h test/redirect.h
$(CXX) $(CPPFLAGS) $(CXXFLAGS) ${INCLUDE_FOR_TEST} -c -o test/testmemleak.o test/testmemleak.cpp
test/testnonreentrantfunctions.o: test/testnonreentrantfunctions.cpp lib/tokenize.h lib/checknonreentrantfunctions.h lib/check.h lib/token.h lib/settings.h lib/errorlogger.h test/testsuite.h test/redirect.h
$(CXX) $(CPPFLAGS) $(CXXFLAGS) ${INCLUDE_FOR_TEST} -c -o test/testnonreentrantfunctions.o test/testnonreentrantfunctions.cpp
test/testnullpointer.o: test/testnullpointer.cpp lib/tokenize.h lib/checknullpointer.h lib/check.h lib/token.h lib/settings.h lib/errorlogger.h test/testsuite.h test/redirect.h
$(CXX) $(CPPFLAGS) $(CXXFLAGS) ${INCLUDE_FOR_TEST} -c -o test/testnullpointer.o test/testnullpointer.cpp

View File

@ -0,0 +1,59 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2011 Daniel Marjamäki and Cppcheck team.
*
* 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/>.
*/
//---------------------------------------------------------------------------
// Find non reentrant functions
//---------------------------------------------------------------------------
#include "checknonreentrantfunctions.h"
//---------------------------------------------------------------------------
// Register this check class (by creating a static instance of it)
namespace
{
CheckNonReentrantFunctions instance;
}
void CheckNonReentrantFunctions::nonReentrantFunctions()
{
if (!_settings->_checkCodingStyle)
return;
// Don't check C# and Java code
if (_tokenizer->isJavaOrCSharp())
return;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
std::list< std::pair<const std::string, const std::string> >::const_iterator it(_nonReentrantFunctions.begin()), itend(_nonReentrantFunctions.end());
for (; it!=itend; ++it)
{
if (tok->strAt(1) == it->first && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && !Token::Match(tok, ".|::|:|,"))
{
// If checking an code that is single threaded, this might be not interesing.
// Therefore this is "style"
reportError(tok->tokAt(1), Severity::style, "nonreentrantFunctions"+it->first, it->second);
break;
}
}
}
}
//---------------------------------------------------------------------------

View File

@ -0,0 +1,114 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2011 Daniel Marjamäki and Cppcheck team.
*
* 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 CheckNonReentrantFunctionsH
#define CheckNonReentrantFunctionsH
//---------------------------------------------------------------------------
#include "check.h"
#include <string>
#include <list>
/// @addtogroup Checks
/// @{
/**
* @brief Using non reentrant functions that can be replaced by their reentrant versions
*/
class CheckNonReentrantFunctions : public Check
{
public:
/** This constructor is used when registering the CheckNonReentrantFunctions */
CheckNonReentrantFunctions() : Check(myName())
{
initNonReentrantFunctions();
}
/** This constructor is used when running checks. */
CheckNonReentrantFunctions(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger)
{
initNonReentrantFunctions();
}
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{
CheckNonReentrantFunctions checkNonReentrantFunctions(tokenizer, settings, errorLogger);
checkNonReentrantFunctions.nonReentrantFunctions();
}
/** Check for non reentrant functions */
void nonReentrantFunctions();
private:
/* function name / error message */
std::list< std::pair< const std::string, const std::string> > _nonReentrantFunctions;
/** init obsolete functions list ' */
void initNonReentrantFunctions()
{
_nonReentrantFunctions.push_back(std::make_pair("crypt","Found the non reentrant function 'crpyt'. For threadsafe applications it is recommended to use the reentrant replacement function 'crypt_r'"));
_nonReentrantFunctions.push_back(std::make_pair("getlogin","Found the non reentrant function 'getlogin'. For threadsafe applications it is recommended to use the reentrant replacement function 'getlogin_r'"));
_nonReentrantFunctions.push_back(std::make_pair("ttyname","Found the non reentrant function 'ttyname'. For threadsafe applications it is recommended to use the reentrant replacement function 'ttyname_r'"));
_nonReentrantFunctions.push_back(std::make_pair("asctime","Found the non reentrant function 'asctime'. For threadsafe applications it is recommended to use the reentrant replacement function 'asctime_r'"));
_nonReentrantFunctions.push_back(std::make_pair("ctime","Found the non reentrant function 'ctime'. For threadsafe applications it is recommended to use the reentrant replacement function 'ctime_r'"));
_nonReentrantFunctions.push_back(std::make_pair("gmtime","Found the non reentrant function 'gmtime'. For threadsafe applications it is recommended to use the reentrant replacement function 'gmtime_r'"));
_nonReentrantFunctions.push_back(std::make_pair("localtime","Found the non reentrant function 'localtime'. For threadsafe applications it is recommended to use the reentrant replacement function 'localtime_r'"));
_nonReentrantFunctions.push_back(std::make_pair("getgrgid","Found the non reentrant function 'getgrgid'. For threadsafe applications it is recommended to use the reentrant replacement function 'getgrgid_r'"));
_nonReentrantFunctions.push_back(std::make_pair("getgrnam","Found the non reentrant function 'getgrnam'. For threadsafe applications it is recommended to use the reentrant replacement function 'getgrnam_r'"));
_nonReentrantFunctions.push_back(std::make_pair("getpwnam","Found the non reentrant function 'getpwnam'. For threadsafe applications it is recommended to use the reentrant replacement function 'getpwnam_r'"));
_nonReentrantFunctions.push_back(std::make_pair("getpwuid","Found the non reentrant function 'getpwuid'. For threadsafe applications it is recommended to use the reentrant replacement function 'getpwuid_r'"));
_nonReentrantFunctions.push_back(std::make_pair("rand","Found the non reentrant function 'rand'. For threadsafe applications it is recommended to use the reentrant replacement function 'rand_r'"));
}
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
CheckNonReentrantFunctions c(0, settings, errorLogger);
std::list< std::pair<const std::string, const std::string> >::const_iterator it(_nonReentrantFunctions.begin()), itend(_nonReentrantFunctions.end());
for (; it!=itend; ++it)
{
c.reportError(0, Severity::style, "nonreentrantFunctions"+it->first, it->second);
}
}
std::string myName() const
{
return "Non reentrant functions";
}
std::string classInfo() const
{
std::string info = "Warn if any of these non reentrant functions are used:\n";
std::list< std::pair<const std::string, const std::string> >::const_iterator it(_nonReentrantFunctions.begin()), itend(_nonReentrantFunctions.end());
for (; it!=itend; ++it)
{
info += "* " + it->first + "\n";
}
return info;
}
};
/// @}
//---------------------------------------------------------------------------
#endif

View File

@ -9,6 +9,7 @@ HEADERS += $${BASEPATH}check.h \
$${BASEPATH}checkclass.h \
$${BASEPATH}checkexceptionsafety.h \
$${BASEPATH}checkmemoryleak.h \
$${BASEPATH}checknonreentrantfunctions.h \
$${BASEPATH}checknullpointer.h \
$${BASEPATH}checkobsoletefunctions.h \
$${BASEPATH}checkother.h \
@ -34,6 +35,7 @@ SOURCES += $${BASEPATH}check64bit.cpp \
$${BASEPATH}checkclass.cpp \
$${BASEPATH}checkexceptionsafety.cpp \
$${BASEPATH}checkmemoryleak.cpp \
$${BASEPATH}checknonreentrantfunctions.cpp \
$${BASEPATH}checknullpointer.cpp \
$${BASEPATH}checkobsoletefunctions.cpp \
$${BASEPATH}checkother.cpp \

View File

@ -0,0 +1,97 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2011 Daniel Marjamäki and Cppcheck team.
*
* 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 "checknonreentrantfunctions.h"
#include "testsuite.h"
#include <sstream>
extern std::ostringstream errout;
class TestNonReentrantFunctions : public TestFixture
{
public:
TestNonReentrantFunctions() : TestFixture("TestNonReentrantFunctions")
{ }
private:
void run()
{
TEST_CASE(test_crypt);
}
void check(const char code[])
{
// Clear the error buffer..
errout.str("");
Settings settings;
settings._checkCodingStyle = true;
settings.inconclusive = true;
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
// Assign variable ids
tokenizer.setVarId();
// Fill function list
tokenizer.fillFunctionList();
// Check for non reentrant functions..
CheckNonReentrantFunctions checkNonReentrantFunctions(&tokenizer, &settings, this);
checkNonReentrantFunctions.nonReentrantFunctions();
}
void test_crypt()
{
check("void f(char *pwd)\n"
"{\n"
" char *cpwd;"
" crypt(pwd, cpwd);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Found the non reentrant function 'crpyt'. For threadsafe applications it is recommended to use the reentrant replacement function 'crypt_r'\n", errout.str());
check("void f()\n"
"{\n"
" char *pwd = getpass(\"Password:\");"
" char *cpwd;"
" crypt(pwd, cpwd);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Found the non reentrant function 'crpyt'. For threadsafe applications it is recommended to use the reentrant replacement function 'crypt_r'\n", errout.str());
check("int f()\n"
"{\n"
" int crypt = 0;"
" return crypt;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
};
REGISTER_TEST(TestNonReentrantFunctions)