Fixed #4983 (False positive warning 'Possible null pointer dereference: lblList - otherwise it is redundant to check it against null.' with sizeof lblList[0])

This commit is contained in:
Daniel Marjamäki 2013-11-02 23:56:10 +01:00
parent 4d45538803
commit 5f0cfa0ee4
4 changed files with 49 additions and 1 deletions

View File

@ -1675,6 +1675,8 @@ bool Tokenizer::tokenize(std::istream &code,
if (!simplifyAddBraces())
return false;
sizeofAddParentheses();
// Combine tokens..
combineOperators();
@ -3207,6 +3209,42 @@ void Tokenizer::createLinks2()
}
}
void Tokenizer::sizeofAddParentheses()
{
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "sizeof %var%")) {
Token *tempToken = tok->next();
while (Token::Match(tempToken, "%var%")) {
while (tempToken && tempToken->next() && tempToken->next()->str() == "[")
tempToken = tempToken->next()->link();
if (!tempToken || !tempToken->next())
break;
if (Token::Match(tempToken->next(), ". %var%")) {
// We are checking a class or struct, search next varname
tempToken = tempToken->tokAt(2);
continue;
} else if (tempToken->next()->type() == Token::eIncDecOp) {
// We have variable++ or variable--, the sizeof argument
// ends after the op
tempToken = tempToken->next();
} else if (Token::Match(tempToken->next(), "[),;}]")) {
;
} else {
break;
}
// Ok. Add ( after sizeof and ) after tempToken
tok->insertToken("(");
tempToken->insertToken(")");
Token::createMutualLinks(tok->next(), tempToken->next());
break;
}
}
}
}
bool Tokenizer::simplifySizeof()
{
// Locate variable declarations and calculate the size

View File

@ -196,6 +196,9 @@ public:
*/
void simplifyRealloc();
/** Add parentheses for sizeof: sizeof x => sizeof(x) */
void sizeofAddParentheses();
/**
* Replace sizeof() to appropriate size.
* @return true if modifications to token-list are done.

View File

@ -803,6 +803,13 @@ private:
"}", true, "test.cpp", false);
ASSERT_EQUALS("", errout.str());
check("void f(type* p) {\n" // #4983
" x(sizeof p[0]);\n"
" if (!p)\n"
" ;\n"
"}", false, "test.cpp", false);
ASSERT_EQUALS("", errout.str());
// #3023 - checked deref
check("void f(struct ABC *abc) {\n"
" WARN_ON(!abc || abc->x == 0);\n"

View File

@ -579,7 +579,7 @@ private:
void tokenize2() {
const std::string code("{ sizeof a, sizeof b }");
ASSERT_EQUALS("{ sizeof a , sizeof b }", tokenizeAndStringify(code.c_str()));
ASSERT_EQUALS("{ sizeof ( a ) , sizeof ( b ) }", tokenizeAndStringify(code.c_str()));
}
void tokenize3() {