Skip to content

Commit 85acb00

Browse files
author
Daniel Marjamäki
committed
Fixed cppcheck-opensource#1941 (Internal error::Space Info::getVarList found variable with varid 0)
1 parent 296289d commit 85acb00

4 files changed

Lines changed: 27 additions & 14 deletions

File tree

lib/checkobsoletefunctions.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ void CheckObsoleteFunctions::obsoleteFunctions()
3838

3939
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
4040
{
41-
std::list< std::pair<const std::string, const std::string> >::const_iterator it (_obsoleteFunctions.begin()), itend(_obsoleteFunctions.end());
42-
for(;it!=itend;++it) {
41+
std::list< std::pair<const std::string, const std::string> >::const_iterator it(_obsoleteFunctions.begin()), itend(_obsoleteFunctions.end());
42+
for (; it!=itend; ++it)
43+
{
4344
if (tok->strAt(1) == it->first && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
4445
{
4546
reportError(tok->tokAt(1), Severity::style, "obsoleteFunctions"+it->first, it->second);

lib/checkobsoletefunctions.h

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,16 @@ class CheckObsoleteFunctions : public Check
3939
public:
4040
/** This constructor is used when registering the CheckObsoleteFunctions */
4141
CheckObsoleteFunctions() : Check()
42-
{ initObsoleteFunctions(); }
42+
{
43+
initObsoleteFunctions();
44+
}
4345

4446
/** This constructor is used when running checks. */
4547
CheckObsoleteFunctions(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
4648
: Check(tokenizer, settings, errorLogger)
47-
{ initObsoleteFunctions(); }
49+
{
50+
initObsoleteFunctions();
51+
}
4852

4953
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
5054
{
@@ -56,11 +60,12 @@ class CheckObsoleteFunctions : public Check
5660
void obsoleteFunctions();
5761

5862
private:
59-
/* function name / error message */
63+
/* function name / error message */
6064
std::list< std::pair< const std::string, const std::string> > _obsoleteFunctions;
6165

6266
/** init obsolete functions list ' */
63-
void initObsoleteFunctions() {
67+
void initObsoleteFunctions()
68+
{
6469
_obsoleteFunctions.push_back(std::make_pair("bsd_signal","Found obsolete function 'bsd_signal'. It is recommended that new applications use the 'sigaction' function"));
6570

6671
_obsoleteFunctions.push_back(std::make_pair("gethostbyaddr","Found obsolete function 'gethostbyaddr'. It is recommended that new applications use the 'getaddrinfo' function"));
@@ -71,7 +76,7 @@ class CheckObsoleteFunctions : public Check
7176
_obsoleteFunctions.push_back(std::make_pair("bcmp","Found obsolete function 'bcmp'. It is recommended that new applications use the 'memcmp' function"));
7277
_obsoleteFunctions.push_back(std::make_pair("bcopy","Found obsolete function 'bcopy'. It is recommended that new applications use the 'memmove' function"));
7378
_obsoleteFunctions.push_back(std::make_pair("bzero","Found obsolete function 'bzero'. It is recommended that new applications use the 'memset' function"));
74-
79+
7580
_obsoleteFunctions.push_back(std::make_pair("ecvt","Found obsolete function 'ecvt'. It is recommended that new applications use the 'sprintf' function"));
7681
_obsoleteFunctions.push_back(std::make_pair("fcvt","Found obsolete function 'fcvt'. It is recommended that new applications use the 'sprintf' function"));
7782
_obsoleteFunctions.push_back(std::make_pair("gcvt","Found obsolete function 'gcvt'. It is recommended that new applications use the 'sprintf' function"));
@@ -102,8 +107,9 @@ class CheckObsoleteFunctions : public Check
102107

103108
void getErrorMessages()
104109
{
105-
std::list< std::pair<const std::string, const std::string> >::const_iterator it (_obsoleteFunctions.begin()), itend(_obsoleteFunctions.end());
106-
for(;it!=itend;++it) {
110+
std::list< std::pair<const std::string, const std::string> >::const_iterator it(_obsoleteFunctions.begin()), itend(_obsoleteFunctions.end());
111+
for (; it!=itend; ++it)
112+
{
107113
reportError(0, Severity::style, "obsoleteFunctions"+it->first, it->second);
108114
}
109115
}
@@ -116,8 +122,9 @@ class CheckObsoleteFunctions : public Check
116122
std::string classInfo() const
117123
{
118124
std::string info = "Warn if any of these obsolete functions are used:\n";
119-
std::list< std::pair<const std::string, const std::string> >::const_iterator it (_obsoleteFunctions.begin()), itend(_obsoleteFunctions.end());
120-
for(;it!=itend;++it) {
125+
std::list< std::pair<const std::string, const std::string> >::const_iterator it(_obsoleteFunctions.begin()), itend(_obsoleteFunctions.end());
126+
for (; it!=itend; ++it)
127+
{
121128
info += "* " + it->first + "\n";
122129
}
123130
return info;

lib/tokenize.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7863,8 +7863,10 @@ void Tokenizer::simplifyBitfields()
78637863
{
78647864
for (Token *tok = _tokens; tok; tok = tok->next())
78657865
{
7866-
if (Token::Match(tok, "[;{] int|signed|unsigned %var% : %num% ;"))
7866+
if (Token::Match(tok, "[;{] signed|unsigned|int|long %var% : %num% ;"))
78677867
Token::eraseTokens(tok->tokAt(2), tok->tokAt(5));
7868+
if (Token::Match(tok, "[;{] signed|unsigned int|long %var% : %num% ;"))
7869+
Token::eraseTokens(tok->tokAt(3), tok->tokAt(6));
78687870
}
78697871
}
78707872

test/testtokenize.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4143,8 +4143,11 @@ class TestTokenizer : public TestFixture
41434143

41444144
void bitfields()
41454145
{
4146-
const char code[] = "struct A { int x : 3; };";
4147-
ASSERT_EQUALS("struct A { int x ; } ;", tokenizeAndStringify(code,false));
4146+
const char code1[] = "struct A { int x : 3; };";
4147+
ASSERT_EQUALS("struct A { int x ; } ;", tokenizeAndStringify(code1,false));
4148+
4149+
const char code2[] = "struct A { unsigned long x : 3; };";
4150+
ASSERT_EQUALS("struct A { unsigned long x ; } ;", tokenizeAndStringify(code2,false));
41484151
}
41494152
};
41504153

0 commit comments

Comments
 (0)