added a check to detect nonreentrant functions and a --posix switch

This commit is contained in:
Ettl Martin 2011-07-29 18:27:01 +02:00
parent 35bb5dffa4
commit 6b23dd9928
5 changed files with 27 additions and 22 deletions

View File

@ -28,12 +28,12 @@
// Register this check class (by creating a static instance of it) // Register this check class (by creating a static instance of it)
namespace namespace
{ {
CheckNonReentrantFunctions instance; CheckNonReentrantFunctions instance;
} }
void CheckNonReentrantFunctions::nonReentrantFunctions() void CheckNonReentrantFunctions::nonReentrantFunctions()
{ {
if (!_settings->_checkCodingStyle) if (!_settings->_posix || !_settings->_checkCodingStyle)
return; return;
// Don't check C# and Java code // Don't check C# and Java code
@ -47,9 +47,9 @@ void CheckNonReentrantFunctions::nonReentrantFunctions()
{ {
if (tok->strAt(1) == it->first && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && !Token::Match(tok, ".|::|:|,")) 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. // If checking code that is single threaded, this might be not interesing for all.
// Therefore this is "style" // Therefore this is "portabiblity"
reportError(tok->tokAt(1), Severity::style, "nonreentrantFunctions"+it->first, it->second); reportError(tok->tokAt(1), Severity::portability, "nonreentrantFunctions"+it->first, it->second);
break; break;
} }
} }

View File

@ -64,21 +64,21 @@ private:
/* function name / error message */ /* function name / error message */
std::list< std::pair< const std::string, const std::string> > _nonReentrantFunctions; std::list< std::pair< const std::string, const std::string> > _nonReentrantFunctions;
/** init obsolete functions list ' */ /** init nonreentrant functions list ' */
void initNonReentrantFunctions() 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("crypt","Found non reentrant function 'crypt'. 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("getlogin","Found 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("ttyname","Found 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("asctime","Found 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("ctime","Found 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("gmtime","Found 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("localtime","Found 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("getgrgid","Found 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("getgrnam","Found 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("getpwnam","Found 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("getpwuid","Found 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'")); _nonReentrantFunctions.push_back(std::make_pair("rand","Found 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) void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
@ -88,7 +88,7 @@ private:
std::list< std::pair<const std::string, const std::string> >::const_iterator it(_nonReentrantFunctions.begin()), itend(_nonReentrantFunctions.end()); std::list< std::pair<const std::string, const std::string> >::const_iterator it(_nonReentrantFunctions.begin()), itend(_nonReentrantFunctions.end());
for (; it!=itend; ++it) for (; it!=itend; ++it)
{ {
c.reportError(0, Severity::style, "nonreentrantFunctions"+it->first, it->second); c.reportError(0, Severity::portability, "nonreentrantFunctions"+it->first, it->second);
} }
} }

View File

@ -36,6 +36,7 @@ Settings::Settings()
_inlineSuppressions = false; _inlineSuppressions = false;
_verbose = false; _verbose = false;
_force = false; _force = false;
_posix = false;
_xml = false; _xml = false;
_xml_version = 1; _xml_version = 1;
_jobs = 1; _jobs = 1;

View File

@ -96,6 +96,9 @@ public:
/** @brief write XML results (--xml) */ /** @brief write XML results (--xml) */
bool _xml; bool _xml;
/** @brief check posix functions (--posix) */
bool _posix;
/** @brief XML version (--xmlver=..) */ /** @brief XML version (--xmlver=..) */
int _xml_version; int _xml_version;

View File

@ -45,8 +45,9 @@ private:
errout.str(""); errout.str("");
Settings settings; Settings settings;
settings._checkCodingStyle = true; settings._posix = true;
settings.inconclusive = true; settings.inconclusive = true;
settings._checkCodingStyle = true;
// Tokenize.. // Tokenize..
Tokenizer tokenizer(&settings, this); Tokenizer tokenizer(&settings, this);
@ -72,7 +73,7 @@ private:
" char *cpwd;" " char *cpwd;"
" crypt(pwd, cpwd);\n" " crypt(pwd, cpwd);\n"
"}\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()); ASSERT_EQUALS("[test.cpp:3]: (portability) Found non reentrant function 'crypt'. For threadsafe applications it is recommended to use the reentrant replacement function 'crypt_r'\n", errout.str());
check("void f()\n" check("void f()\n"
"{\n" "{\n"
@ -80,7 +81,7 @@ private:
" char *cpwd;" " char *cpwd;"
" crypt(pwd, cpwd);\n" " crypt(pwd, cpwd);\n"
"}\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()); ASSERT_EQUALS("[test.cpp:3]: (portability) Found non reentrant function 'crypt'. For threadsafe applications it is recommended to use the reentrant replacement function 'crypt_r'\n", errout.str());
check("int f()\n" check("int f()\n"
"{\n" "{\n"