From 6b23dd992868bbd5463d3f6f66a421702da13ff1 Mon Sep 17 00:00:00 2001 From: Ettl Martin Date: Fri, 29 Jul 2011 18:27:01 +0200 Subject: [PATCH] added a check to detect nonreentrant functions and a --posix switch --- lib/checknonreentrantfunctions.cpp | 10 +++++----- lib/checknonreentrantfunctions.h | 28 ++++++++++++++-------------- lib/settings.cpp | 1 + lib/settings.h | 3 +++ test/testnonreentrantfunctions.cpp | 7 ++++--- 5 files changed, 27 insertions(+), 22 deletions(-) diff --git a/lib/checknonreentrantfunctions.cpp b/lib/checknonreentrantfunctions.cpp index 3202fc459..7acd88c75 100644 --- a/lib/checknonreentrantfunctions.cpp +++ b/lib/checknonreentrantfunctions.cpp @@ -28,12 +28,12 @@ // Register this check class (by creating a static instance of it) namespace { - CheckNonReentrantFunctions instance; +CheckNonReentrantFunctions instance; } void CheckNonReentrantFunctions::nonReentrantFunctions() { - if (!_settings->_checkCodingStyle) + if (!_settings->_posix || !_settings->_checkCodingStyle) return; // 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 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); + // If checking code that is single threaded, this might be not interesing for all. + // Therefore this is "portabiblity" + reportError(tok->tokAt(1), Severity::portability, "nonreentrantFunctions"+it->first, it->second); break; } } diff --git a/lib/checknonreentrantfunctions.h b/lib/checknonreentrantfunctions.h index 2d50dae0f..1c4d940cc 100644 --- a/lib/checknonreentrantfunctions.h +++ b/lib/checknonreentrantfunctions.h @@ -64,21 +64,21 @@ private: /* function name / error message */ std::list< std::pair< const std::string, const std::string> > _nonReentrantFunctions; - /** init obsolete functions list ' */ + /** init nonreentrant 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'")); + _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 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 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 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 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 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 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 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 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 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 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 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) @@ -88,7 +88,7 @@ private: std::list< std::pair >::const_iterator it(_nonReentrantFunctions.begin()), itend(_nonReentrantFunctions.end()); for (; it!=itend; ++it) { - c.reportError(0, Severity::style, "nonreentrantFunctions"+it->first, it->second); + c.reportError(0, Severity::portability, "nonreentrantFunctions"+it->first, it->second); } } diff --git a/lib/settings.cpp b/lib/settings.cpp index c51fac185..d14a24eba 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -36,6 +36,7 @@ Settings::Settings() _inlineSuppressions = false; _verbose = false; _force = false; + _posix = false; _xml = false; _xml_version = 1; _jobs = 1; diff --git a/lib/settings.h b/lib/settings.h index 0ad9426cc..cf137895f 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -96,6 +96,9 @@ public: /** @brief write XML results (--xml) */ bool _xml; + /** @brief check posix functions (--posix) */ + bool _posix; + /** @brief XML version (--xmlver=..) */ int _xml_version; diff --git a/test/testnonreentrantfunctions.cpp b/test/testnonreentrantfunctions.cpp index c79860d65..70b508cb0 100644 --- a/test/testnonreentrantfunctions.cpp +++ b/test/testnonreentrantfunctions.cpp @@ -45,8 +45,9 @@ private: errout.str(""); Settings settings; - settings._checkCodingStyle = true; + settings._posix = true; settings.inconclusive = true; + settings._checkCodingStyle = true; // Tokenize.. Tokenizer tokenizer(&settings, this); @@ -72,7 +73,7 @@ private: " 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()); + 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" "{\n" @@ -80,7 +81,7 @@ private: " 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()); + 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" "{\n"