diff --git a/lib/vf_analyzers.cpp b/lib/vf_analyzers.cpp index ccadc3ef623..eb91b755e13 100644 --- a/lib/vf_analyzers.cpp +++ b/lib/vf_analyzers.cpp @@ -1349,10 +1349,127 @@ struct ExpressionAnalyzer : SingleValueFlowAnalyzer { } static bool nonLocal(const Variable* var, bool deref) { - return !var || (!var->isLocal() && !var->isArgument()) || (deref && var->isArgument() && var->isPointer()) || + return !var || (!var->isLocal() && !var->isArgument()) || (deref && var->isPointer()) || var->isStatic() || var->isReference() || var->isExtern(); } + // Is the expression that dereferences the pointer only read or written, so that the + // address of the object (or its members) does not escape? + static bool isLocalDerefUse(const Token* deref) { + const Token* parent = deref->astParent(); + if (!parent) + return true; + if (parent->isUnaryOp("&")) + return false; + // member function call, 'this' might escape + if (Token::Match(deref, ". %name% (")) + return false; + // passed to function, might be passed by reference + if (Token::Match(parent, ",|(") && !parent->isCast() && !(parent->str() == "(" && Token::Match(parent->previous(), "if|while|switch"))) + return false; + if (Token::Match(parent, "{|:|return|throw")) + return false; + // bound to reference + if (parent->str() == "=" && astIsRHS(deref) && parent->astOperand1() && parent->astOperand1()->variable() && + parent->astOperand1()->variable()->isReference()) + return false; + return true; + } + + // Skip pointer casts, e.g. '(S*)malloc(n)' or 'static_cast(malloc(n))' + static const Token* skipPointerCasts(const Token* tok) { + while (Token::simpleMatch(tok, "(")) { + if (tok->isCast()) + tok = tok->astOperand2() ? tok->astOperand2() : tok->astOperand1(); + else if (Token::Match(tok->astOperand1(), "static_cast|reinterpret_cast")) + tok = tok->astOperand2(); + else + break; + } + return tok; + } + + // Is the memory allocated by a library allocation function, e.g. 'malloc(n)'? + static bool isLibraryMemoryAllocation(const Token* tok, const Settings& settings) { + tok = skipPointerCasts(tok); + // the function name is the token before '(', also for 'std::malloc(n)' + if (!Token::simpleMatch(tok, "(") || !tok->astOperand1() || !Token::Match(tok->previous(), "%name% (")) + return false; + const Library::AllocFunc* allocFunc = settings.library.getAllocFuncInfo(tok->previous()); + return allocFunc && Library::ismemory(allocFunc); + } + + // Does the local pointer point at an object allocated with 'new' or a library allocation function + // (e.g. malloc) that can't be accessed outside the function? + static bool pointsToLocalAllocation(const Variable* var, const Settings& settings) { + if (!var || !var->isLocal() || var->isStatic() || !var->isPointer() || var->isReference() || var->isArray()) + return false; + const Token* assignTok = var->nameToken()->next(); + if (Token::Match(assignTok, "; %varid% =", var->declarationId())) // declaration is split + assignTok = assignTok->tokAt(2); + if (!Token::simpleMatch(assignTok, "=") || !var->scope() || !var->scope()->bodyEnd) + return false; + const Token* allocTok = assignTok->astOperand2(); + if (allocTok && allocTok->isKeyword() && allocTok->str() == "new") { + // The constructor might store the address of the object + const Token* typeTok = allocTok->next(); + while (Token::Match(typeTok, "%name% ::")) + typeTok = typeTok->tokAt(2); + if (!Token::Match(typeTok, "%type%")) + return false; // placement new + if (!typeTok->isStandardType()) { + const Type* type = typeTok->type(); + if (!type || !type->classScope || type->classScope->numConstructors > 0 || !type->derivedFrom.empty()) + return false; + } + } else if (!isLibraryMemoryAllocation(allocTok, settings)) { + // No constructor is called for memory from an allocation function such as malloc + return false; + } + for (const Token* tok = assignTok; tok != var->scope()->bodyEnd; tok = tok->next()) { + if (tok->varId() != var->declarationId()) + continue; + // The pointer is used in a lambda that might be called by another function + for (const Scope* scope = tok->scope(); scope && scope != var->scope(); scope = scope->nestedIn) { + if (scope->type == ScopeType::eLambda) + return false; + } + const Token* parent = tok->astParent(); + if (!parent) + return false; + if (Token::Match(parent, "delete|!|%comp%|&&|%oror%")) + continue; + if (parent->str() == "(" && Token::Match(parent->previous(), "if|while (")) + continue; + // deallocated with a library function, e.g. 'free(p)' + if (parent->str() == "(" && parent->astOperand2() == tok && parent->astOperand1() && Token::Match(parent->previous(), "%name% (") && + settings.library.getDeallocFuncInfo(parent->previous())) + continue; + if ((parent->isUnaryOp("*") || (parent->originalName() == "->" && astIsLHS(tok)) || + (parent->str() == "[" && astIsLHS(tok))) && isLocalDerefUse(parent)) + continue; + return false; + } + return true; + } + + // Does the pointer point at a known local object? + static bool pointsToLocal(const Token* tok, const Settings& settings) { + if (pointsToLocalAllocation(tok->variable(), settings)) + return true; + bool result = false; + for (const ValueFlow::Value& v : tok->values()) { + if (!v.isLifetimeValue()) + continue; + if (!v.isLocalLifetimeValue() || v.isInconclusive() || v.lifetimeKind != ValueFlow::Value::LifetimeKind::Address) + return false; + if (!v.tokvalue || !v.tokvalue->variable() || nonLocal(v.tokvalue->variable(), false)) + return false; + result = true; + } + return result; + } + void setupExprVarIds(const Token* start, int depth = 0) { if (depth > settings().vfOptions.maxExprVarIdDepth) { // TODO: add bailout message @@ -1388,8 +1505,9 @@ struct ExpressionAnalyzer : SingleValueFlowAnalyzer { return ChildrenToVisit::none; const bool deref = tok->astParent() && (tok->astParent()->isUnaryOp("*") || - (tok->astParent()->str() == "[" && tok == tok->astParent()->astOperand1())); - local &= !nonLocal(tok->variable(), deref); + (tok->astParent()->str() == "[" && tok == tok->astParent()->astOperand1()) || + (tok->astParent()->originalName() == "->" && tok == tok->astParent()->astOperand1())); + local &= !nonLocal(tok->variable(), deref && !pointsToLocal(tok, settings())); } } return ChildrenToVisit::op1_and_op2; diff --git a/releasenotes.txt b/releasenotes.txt index e70c9230171..3cf113efda9 100644 --- a/releasenotes.txt +++ b/releasenotes.txt @@ -26,4 +26,4 @@ Infrastructure & dependencies: and rule files work unchanged. On Windows the Visual Studio project now expects pcre2-8.lib and pcre2.h in externals. Other: -- +- Fixed false positive knownConditionTrueFalse when a member is read through a pointer ('p->i') and a function call in between may change the object that the pointer points at (#12795). diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 5961dca21a9..14c7ddf25ca 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -5295,9 +5295,138 @@ class TestCondition : public TestFixture { " t.modify();\n" " if (p->i == o) {}\n" "}\n"); - TODO_ASSERT_EQUALS("", - "[test.cpp:11:14]: (style) Condition 'p->i==o' is always true [knownConditionTrueFalse]\n", - errout_str()); + ASSERT_EQUALS("", errout_str()); + + // the object pointed at might be changed by the function call + check("struct S { int i; };\n" + "void modify();\n" + "void f(const S* p) {\n" + " const int o = p->i;\n" + " modify();\n" + " if (p->i == o) {}\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + + check("struct S { int i; };\n" + "void modify();\n" + "const S* get();\n" + "void f() {\n" + " const S* p = get();\n" + " const int o = p->i;\n" + " modify();\n" + " if (p->i == o) {}\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + + // the pointer points at a local object + check("struct S { int i; };\n" + "void modify();\n" + "void f() {\n" + " S s{};\n" + " const S* p = &s;\n" + " const int o = p->i;\n" + " modify();\n" + " if (p->i == o) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:8:14]: (style) Condition 'p->i==o' is always true [knownConditionTrueFalse]\n", errout_str()); + + // the pointer points at an allocated object that can't be accessed by other code + check("struct S { int i; };\n" + "void modify();\n" + "void f() {\n" + " S* p = new S{};\n" + " const int o = p->i;\n" + " modify();\n" + " if (p->i == o) {}\n" + " delete p;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:7:14]: (style) Condition 'p->i==o' is always true [knownConditionTrueFalse]\n", errout_str()); + + // the allocated object escapes + check("struct S { int i; };\n" + "void modify();\n" + "void g(S*);\n" + "void f() {\n" + " S* p = new S{};\n" + " g(p);\n" + " const int o = p->i;\n" + " modify();\n" + " if (p->i == o) {}\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + + check("struct S { int i; };\n" + "void modify();\n" + "void g(int&);\n" + "void f() {\n" + " S* p = new S{};\n" + " g(p->i);\n" + " const int o = p->i;\n" + " modify();\n" + " if (p->i == o) {}\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + + check("struct S { int i; };\n" + "void modify();\n" + "S* f() {\n" + " S* p = new S{};\n" + " const int o = p->i;\n" + " modify();\n" + " if (p->i == o) {}\n" + " return p;\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + + // the constructor might store the address of the object + check("struct S { S(); int i; };\n" + "void modify();\n" + "void f() {\n" + " S* p = new S;\n" + " const int o = p->i;\n" + " modify();\n" + " if (p->i == o) {}\n" + " delete p;\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + + // memory from a library allocation function that can't be accessed outside the function + check("struct S { int i; };\n" + "void modify(void);\n" + "void f(void) {\n" + " struct S* p = calloc(1, sizeof(struct S));\n" + " if (!p) return;\n" + " const int o = p->i;\n" + " modify();\n" + " if (p->i == o) {}\n" + " free(p);\n" + "}\n", dinit(CheckOptions, $.cpp = false)); + ASSERT_EQUALS("[test.c:8:14]: (style) Condition 'p->i==o' is always true [knownConditionTrueFalse]\n", errout_str()); + + check("struct S { int i; };\n" + "void modify();\n" + "void f() {\n" + " S* p = static_cast(std::malloc(sizeof(S)));\n" + " const int o = p->i;\n" + " modify();\n" + " if (p->i == o) {}\n" + " std::free(p);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:7:14]: (style) Condition 'p->i==o' is always true [knownConditionTrueFalse]\n", errout_str()); + + // the allocated memory escapes + check("struct S { int i; };\n" + "void modify(void);\n" + "void g(struct S*);\n" + "void f(void) {\n" + " struct S* p = calloc(1, sizeof(struct S));\n" + " g(p);\n" + " const int o = p->i;\n" + " modify();\n" + " if (p->i == o) {}\n" + " free(p);\n" + "}\n", dinit(CheckOptions, $.cpp = false)); + ASSERT_EQUALS("", errout_str()); check("void f(int x) {\n" // #12320 " int a = 0, b = 0, c = 0;\n"