Skip to content

Commit 6dc5b17

Browse files
committed
Restored files removed in 1cc872f
1 parent 8ccf9ee commit 6dc5b17

3 files changed

Lines changed: 535 additions & 0 deletions

File tree

lib/checkobsolescentfunctions.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2014 Daniel Marjamäki and Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
//---------------------------------------------------------------------------
20+
// Obsolete functions
21+
//---------------------------------------------------------------------------
22+
23+
#include "checkobsolescentfunctions.h"
24+
#include "symboldatabase.h"
25+
26+
//---------------------------------------------------------------------------
27+
28+
29+
// Register this check class (by creating a static instance of it)
30+
namespace {
31+
CheckObsoleteFunctions instance;
32+
}
33+
34+
void CheckObsoleteFunctions::obsoleteFunctions()
35+
{
36+
if (!_settings->isEnabled("style"))
37+
return;
38+
39+
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
40+
41+
// Functions defined somewhere?
42+
for (unsigned int i = 0; i < symbolDatabase->functionScopes.size(); i++) {
43+
const Scope* scope = symbolDatabase->functionScopes[i];
44+
_obsoleteStandardFunctions.erase(scope->className);
45+
_obsoletePosixFunctions.erase(scope->className);
46+
_obsoleteC99Functions.erase(scope->className);
47+
}
48+
49+
for (unsigned int i = 0; i < symbolDatabase->functionScopes.size(); i++) {
50+
const Scope* scope = symbolDatabase->functionScopes[i];
51+
for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) {
52+
if (tok->isName() && tok->varId()==0 && (tok->next() && tok->next()->str() == "(") &&
53+
(!Token::Match(tok->previous(), ".|::") || Token::simpleMatch(tok->tokAt(-2), "std ::"))) {
54+
55+
std::map<std::string,std::string>::const_iterator it = _obsoleteStandardFunctions.find(tok->str());
56+
if (it != _obsoleteStandardFunctions.end()) {
57+
// If checking an old code base it might be uninteresting to update obsolete functions.
58+
reportError(tok, Severity::style, "obsoleteFunctions"+it->first, it->second);
59+
} else {
60+
if (_settings->standards.posix) {
61+
it = _obsoletePosixFunctions.find(tok->str());
62+
if (it != _obsoletePosixFunctions.end()) {
63+
// If checking an old code base it might be uninteresting to update obsolete functions.
64+
reportError(tok, Severity::style, "obsoleteFunctions"+it->first, it->second);
65+
}
66+
}
67+
if (_settings->standards.c >= Standards::C99) {
68+
// alloca : this function is obsolete in C but not in C++ (#4382)
69+
it = _obsoleteC99Functions.find(tok->str());
70+
if (it != _obsoleteC99Functions.end() && !(tok->str() == "alloca" && _tokenizer->isCPP())) {
71+
reportError(tok, Severity::style, "obsoleteFunctions"+it->first, it->second);
72+
}
73+
}
74+
}
75+
}
76+
}
77+
}
78+
}
79+
//---------------------------------------------------------------------------

lib/checkobsolescentfunctions.h

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2014 Daniel Marjamäki and Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
20+
//---------------------------------------------------------------------------
21+
#ifndef checkobsoletefunctionsH
22+
#define checkobsoletefunctionsH
23+
//---------------------------------------------------------------------------
24+
25+
#include "config.h"
26+
#include "check.h"
27+
#include <string>
28+
#include <map>
29+
30+
31+
/// @addtogroup Checks
32+
/// @{
33+
34+
/**
35+
* @brief Using obsolete functions that are always insecure to use.
36+
*/
37+
38+
class CPPCHECKLIB CheckObsoleteFunctions : public Check {
39+
public:
40+
/** This constructor is used when registering the CheckObsoleteFunctions */
41+
CheckObsoleteFunctions() : Check(myName()) {
42+
initObsoleteFunctions();
43+
}
44+
45+
/** This constructor is used when running checks. */
46+
CheckObsoleteFunctions(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
47+
: Check(myName(), tokenizer, settings, errorLogger) {
48+
initObsoleteFunctions();
49+
}
50+
51+
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
52+
CheckObsoleteFunctions checkObsoleteFunctions(tokenizer, settings, errorLogger);
53+
checkObsoleteFunctions.obsoleteFunctions();
54+
}
55+
56+
/** Check for obsolete functions */
57+
void obsoleteFunctions();
58+
59+
private:
60+
/* function name / error message */
61+
std::map<std::string, std::string> _obsoleteStandardFunctions;
62+
std::map<std::string, std::string> _obsoletePosixFunctions;
63+
std::map<std::string, std::string> _obsoleteC99Functions;
64+
65+
/** init obsolete functions list ' */
66+
void initObsoleteFunctions() {
67+
// Obsolete posix functions, which messages suggest only one alternative and doesn't contain additional information.
68+
const struct {
69+
const char* bad;
70+
const char* good;
71+
} posix_stdmsgs[] = {
72+
{"bsd_signal", "sigaction"},
73+
{"gethostbyaddr", "getnameinfo"},
74+
{"gethostbyname", "getaddrinfo"},
75+
{"bcmp", "memcmp"},
76+
{"bzero", "memset"},
77+
{"ecvt", "sprintf"},
78+
{"fcvt", "sprintf"},
79+
{"gcvt", "sprintf"},
80+
{"getwd", "getcwd"},
81+
{"index", "strchr"}, // See #2334 (using the Qt Model/View function 'index')
82+
{"rindex", "strrchr"},
83+
{"pthread_attr_getstackaddr", "pthread_attr_getstack"},
84+
{"pthread_attr_setstackaddr", "pthread_attr_setstack"},
85+
{"vfork", "fork"},
86+
{"wcswcs", "wcsstr"},
87+
{"rand_r", "rand"},
88+
{"utime", "utimensat"},
89+
{"asctime_r", "strftime"},
90+
{"ctime_r", "strftime"}
91+
};
92+
93+
for (std::size_t i = 0; i < (sizeof(posix_stdmsgs) / sizeof(*posix_stdmsgs)); ++i) {
94+
_obsoletePosixFunctions[posix_stdmsgs[i].bad] = "Obsolete function '" + std::string(posix_stdmsgs[i].bad) + "' called. It is recommended to use the function '" + posix_stdmsgs[i].good + "' instead.";
95+
}
96+
97+
_obsoletePosixFunctions["usleep"] = "Obsolete function 'usleep' called. It is recommended to use the 'nanosleep' or 'setitimer' function instead.\n"
98+
"The obsolete function 'usleep' is called. 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.";
99+
100+
_obsoletePosixFunctions["bcopy"] = "Obsolete function 'bcopy' called. It is recommended to use the 'memmove' or 'memcpy' function instead.";
101+
102+
_obsoletePosixFunctions["ftime"] = "Obsolete function 'ftime' called. It is recommended to use time(), gettimeofday() or clock_gettime() instead.";
103+
104+
_obsoletePosixFunctions["getcontext"] = "Obsolete function 'getcontext' called. Due to portability issues, applications are recommended to be rewritten to use POSIX threads.";
105+
_obsoletePosixFunctions["makecontext"] = "Obsolete function 'makecontext' called. Due to portability issues, applications are recommended to be rewritten to use POSIX threads.";
106+
_obsoletePosixFunctions["swapcontext"] = "Obsolete function 'swapcontext' called. Due to portability issues, applications are recommended to be rewritten to use POSIX threads.";
107+
108+
_obsoletePosixFunctions["scalbln"] = "Obsolete function 'scalb' called. It is recommended to use 'scalbln', 'scalblnf' or 'scalblnl' instead.";
109+
110+
_obsoletePosixFunctions["ualarm"] = "Obsolete function 'ualarm' called. It is recommended to use 'timer_create', 'timer_delete', 'timer_getoverrun', 'timer_gettime' or 'timer_settime' instead.";
111+
112+
_obsoletePosixFunctions["tmpnam"] = "Obsolete function 'tmpnam' called. It is recommended to use 'tmpfile', 'mkstemp' or 'mkdtemp' instead.";
113+
114+
_obsoletePosixFunctions["tmpnam_r"] = "Obsolete function 'tmpnam_r' called. It is recommended to use 'tmpfile', 'mkstemp' or 'mkdtemp' instead.";
115+
116+
_obsoleteStandardFunctions["gets"] = "Obsolete function 'gets' called. It is recommended to use the function 'fgets' instead.\n"
117+
"The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun if the input data exceeds the size of the buffer. It is recommended to use the function 'fgets' instead.";
118+
_obsoleteC99Functions["alloca"] = "Obsolete function 'alloca' called. In C99 and later it is recommended to use a variable length array instead.\n"
119+
"The obsolete function 'alloca' is called. In C99 and later it is recommended to use a variable length array or a dynamically allocated array instead. The function 'alloca' is dangerous for many reasons (http://stackoverflow.com/questions/1018853/why-is-alloca-not-considered-good-practice and http://linux.die.net/man/3/alloca).";
120+
_obsoleteC99Functions["asctime"] = "Obsolete function 'asctime' called. It is recommended to use the function 'strftime' instead.";
121+
// ctime is obsolete - it's not threadsafe. but there is no good replacement.
122+
//_obsoleteC99Functions["ctime"] = "Obsolete function 'ctime' called. It is recommended to use the function 'strftime' instead.";
123+
}
124+
125+
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
126+
CheckObsoleteFunctions c(0, settings, errorLogger);
127+
128+
std::map<std::string,std::string>::const_iterator it(_obsoletePosixFunctions.begin()), itend(_obsoletePosixFunctions.end());
129+
for (; it!=itend; ++it) {
130+
c.reportError(0, Severity::style, "obsoleteFunctions"+it->first, it->second);
131+
}
132+
}
133+
134+
static std::string myName() {
135+
return "Obsolete functions";
136+
}
137+
138+
std::string classInfo() const {
139+
std::string info = "Warn if any of these obsolete functions are used:\n";
140+
std::map<std::string,std::string>::const_iterator it(_obsoletePosixFunctions.begin()), itend(_obsoletePosixFunctions.end());
141+
for (; it!=itend; ++it) {
142+
info += "* " + it->first + "\n";
143+
}
144+
return info;
145+
}
146+
};
147+
/// @}
148+
//---------------------------------------------------------------------------
149+
#endif // checkobsoletefunctionsH

0 commit comments

Comments
 (0)