Skip to content

Commit 57d1196

Browse files
committed
Replaced CheckNonReentrantFunctions and CheckObsoleteFunctions by generic CheckFunctions which is based on Library (cppcheck-opensource#6529)
1 parent 517922f commit 57d1196

18 files changed

Lines changed: 956 additions & 547 deletions

cfg/posix.cfg

Lines changed: 592 additions & 8 deletions
Large diffs are not rendered by default.

cfg/std.cfg

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
<not-null/>
9393
<not-uninit/>
9494
</arg>
95+
<warn severity="style" cstd="c99" alternatives="strftime" reason="Obsolete"/>
9596
</function>
9697
<!-- void assert(int expression) -->
9798
<function name="assert">
@@ -1435,12 +1436,14 @@
14351436
</arg>
14361437
</function>
14371438
<!-- char *gets(char *buffer); -->
1438-
<function name="gets">
1439+
<function name="gets,std::gets">
14391440
<noreturn>false</noreturn>
14401441
<leak-ignore/>
14411442
<arg nr="1">
14421443
<not-null/>
14431444
</arg>
1445+
<warn severity="warning">Obsolete function 'gets' called. It is recommended to use 'fgets' instead.
1446+
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.</warn>
14441447
</function>
14451448
<!-- struct tm * gmtime(const time_t *tp); -->
14461449
<function name="gmtime,std::gmtime">

lib/checkfunctions.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2015 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+
// Check functions
21+
//---------------------------------------------------------------------------
22+
23+
#include "checkfunctions.h"
24+
#include "symboldatabase.h"
25+
26+
//---------------------------------------------------------------------------
27+
28+
29+
// Register this check class (by creating a static instance of it)
30+
namespace {
31+
CheckFunctions instance;
32+
}
33+
34+
void CheckFunctions::check()
35+
{
36+
const bool checkAlloca = (_settings->standards.c >= Standards::C99 && _tokenizer->isC()) || _settings->standards.cpp >= Standards::CPP11;
37+
38+
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
39+
for (unsigned int i = 0; i < symbolDatabase->functionScopes.size(); i++) {
40+
const Scope* scope = symbolDatabase->functionScopes[i];
41+
for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) {
42+
if (tok->isName() && tok->varId() == 0 && tok->strAt(1) == "(") {
43+
// alloca() is special as it depends on the code being C or C++, so it is not in Library
44+
if (checkAlloca && Token::Match(tok, "alloca (") && (!tok->function() || tok->function()->nestedIn->type == Scope::eGlobal)) {
45+
if (_tokenizer->isC())
46+
reportError(tok, Severity::warning, "allocaCalled",
47+
"Obsolete function 'alloca' called. In C99 and later it is recommended to use a variable length array instead.\n"
48+
"The obsolete function 'alloca' is called. In C99 and later it is recommended to use a variable length array or "
49+
"a dynamically allocated array instead. The function 'alloca' is dangerous for many reasons "
50+
"(http://stackoverflow.com/questions/1018853/why-is-alloca-not-considered-good-practice and http://linux.die.net/man/3/alloca).");
51+
else
52+
reportError(tok, Severity::warning, "allocaCalled",
53+
"Obsolete function 'alloca' called. In C++11 and later it is recommended to use std::array<> instead.\n"
54+
"The obsolete function 'alloca' is called. In C++11 and later it is recommended to use std::array<> or "
55+
"a dynamically allocated array instead. The function 'alloca' is dangerous for many reasons "
56+
"(http://stackoverflow.com/questions/1018853/why-is-alloca-not-considered-good-practice and http://linux.die.net/man/3/alloca).");
57+
} else {
58+
if (tok->function() && tok->function()->hasBody())
59+
continue;
60+
61+
const Library::WarnInfo* wi = _settings->library.getWarnInfo(tok);
62+
if (wi) {
63+
if (_settings->isEnabled(Severity::toString(wi->severity)) && _settings->standards.c >= wi->standards.c && _settings->standards.cpp >= wi->standards.cpp) {
64+
reportError(tok, wi->severity, tok->str() + "Called", wi->message);
65+
}
66+
}
67+
}
68+
}
69+
}
70+
}
71+
}
Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,55 +16,60 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19+
1920
//---------------------------------------------------------------------------
20-
#ifndef checknonreentrantfunctionsH
21-
#define checknonreentrantfunctionsH
21+
#ifndef checkfunctionsH
22+
#define checkfunctionsH
2223
//---------------------------------------------------------------------------
2324

2425
#include "config.h"
2526
#include "check.h"
2627
#include <string>
27-
#include <map>
2828

2929

3030
/// @addtogroup Checks
3131
/// @{
3232

3333
/**
34-
* @brief Using non reentrant functions that can be replaced by their reentrant versions
34+
* @brief Check for functions which should not be used
3535
*/
3636

37-
class CPPCHECKLIB CheckNonReentrantFunctions : public Check {
37+
class CPPCHECKLIB CheckFunctions : public Check {
3838
public:
39-
/** This constructor is used when registering the CheckNonReentrantFunctions */
40-
CheckNonReentrantFunctions() : Check(myName()) {
39+
/** This constructor is used when registering the CheckFunctions */
40+
CheckFunctions() : Check(myName()) {
4141
}
4242

4343
/** This constructor is used when running checks. */
44-
CheckNonReentrantFunctions(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
44+
CheckFunctions(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
4545
: Check(myName(), tokenizer, settings, errorLogger) {
4646
}
4747

4848
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
49-
CheckNonReentrantFunctions checkNonReentrantFunctions(tokenizer, settings, errorLogger);
50-
checkNonReentrantFunctions.nonReentrantFunctions();
49+
CheckFunctions checkFunctions(tokenizer, settings, errorLogger);
50+
checkFunctions.check();
5151
}
5252

53-
/** Check for non reentrant functions */
54-
void nonReentrantFunctions();
53+
/** Check for functions that should not be used */
54+
void check();
5555

5656
private:
57+
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
58+
CheckFunctions c(0, settings, errorLogger);
5759

58-
static std::string generateErrorMessage(const std::string& function);
59-
60-
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const;
60+
for (std::map<std::string, Library::WarnInfo>::const_iterator i = settings->library.functionwarn.cbegin(); i != settings->library.functionwarn.cend(); ++i) {
61+
c.reportError(0, Severity::style, i->first+"Called", i->second.message);
62+
}
63+
}
6164

6265
static std::string myName() {
63-
return "Non reentrant functions";
66+
return "Check function usage";
6467
}
6568

66-
std::string classInfo() const;
69+
std::string classInfo() const {
70+
return "Warn if a function is called whose usage is discouraged\n";
71+
}
6772
};
6873
/// @}
6974
//---------------------------------------------------------------------------
70-
#endif // checknonreentrantfunctionsH
75+
#endif // checkfunctionsH

lib/checknonreentrantfunctions.cpp

Lines changed: 0 additions & 105 deletions
This file was deleted.

lib/checkobsolescentfunctions.cpp

Lines changed: 0 additions & 76 deletions
This file was deleted.

0 commit comments

Comments
 (0)