assign function pointer: Fixed false positive when pointer is used also. Ticket: #2930

This commit is contained in:
Daniel Marjamki 2013-02-02 15:26:14 +01:00
parent 9c8d600ca9
commit 75e20a996d
2 changed files with 10 additions and 5 deletions

View File

@ -22,7 +22,7 @@
#include "checkautovariables.h"
#include "symboldatabase.h"
#include "checknullpointer.h"
#include "checkuninitvar.h"
#include <list>
#include <string>
@ -80,11 +80,10 @@ static bool checkRvalueExpression(const Variable* var, const Token* next)
return((next->str() != "." || (!var->isPointer() && (!var->isClass() || var->type()))) && next->strAt(2) != ".");
}
static bool pointerIsDereferencedInScope(const Variable *var, const Scope *scope)
static bool pointerIsDereferencedInScope(const Variable *var, const Scope *scope, const bool cpp)
{
bool unknown = false;
for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) {
if (tok->varId() == var->varId() && CheckNullPointer::isPointerDeRef(tok, unknown))
if (tok->varId() == var->varId() && CheckUninitVar::isVariableUsage(tok, true, cpp))
return true;
}
return false;
@ -113,7 +112,7 @@ void CheckAutoVariables::autoVariables()
Token::Match(tok, "[;{}] %var% =") &&
isPtrArg(tok->next()) &&
Token::Match(tok->next()->variable()->typeStartToken(), "struct| %type% * %var% [,)]") &&
!pointerIsDereferencedInScope(tok->next()->variable(), scope)) {
!pointerIsDereferencedInScope(tok->next()->variable(), scope, _tokenizer->isCPP())) {
errorUselessAssignmentPtrArg(tok->next());
} else if (Token::Match(tok, "[;{}] %var% . %var% = & %var%")) {
// TODO: check if the parameter is only changed temporarily (#2969)

View File

@ -252,6 +252,12 @@ private:
" *p = 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo(char* p) {\n"
" if (!p) p = buf;\n"
" do_something(p);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void testautovar_array1() {