Uninitialized variables; Different heuristic needed for const pointer function parameter

This commit is contained in:
Daniel Marjamäki 2021-05-23 11:45:32 +02:00
parent 247b2d8c83
commit 85723f8605
2 changed files with 6 additions and 11 deletions

View File

@ -1148,6 +1148,8 @@ const Token* CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer,
return nullptr; return nullptr;
// FIXME handle address of!! // FIXME handle address of!!
if (valueExpr->astParent()->isUnaryOp("&"))
return nullptr;
if (derefValue && derefValue->astParent() && derefValue->astParent()->isUnaryOp("&")) if (derefValue && derefValue->astParent() && derefValue->astParent()->isUnaryOp("&"))
return nullptr; return nullptr;
@ -1190,14 +1192,6 @@ const Token* CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer,
const int use = isFunctionParUsage(derefValue, false, NO_ALLOC, indirect); const int use = isFunctionParUsage(derefValue, false, NO_ALLOC, indirect);
return (use>0) ? derefValue : nullptr; return (use>0) ? derefValue : nullptr;
} }
if (valueExpr->astParent()->isUnaryOp("&")) {
const Token *parent = valueExpr->astParent();
if (Token::Match(parent->astParent(), "[(,]") && (parent->astParent()->str() == "," || astIsRhs(parent))) {
const int use = isFunctionParUsage(valueExpr, pointer, alloc, indirect);
return (use>0) ? valueExpr : nullptr;
}
return nullptr;
}
// Assignments; // Assignments;
// * Is this LHS in assignment // * Is this LHS in assignment

View File

@ -3051,12 +3051,13 @@ private:
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void a(const char *c);\n" // const address => error checkUninitVar("void a(const char *c);\n" // const address => data is not changed
"void b() {\n" "void b() {\n"
" char c;\n" " char c;\n"
" a(&c);\n" " a(&c);\n" // <- no warning
" c++;\n" // <- uninitialized variable
"}"); "}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str()); TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: c\n", "", errout.str());
// pointer variable // pointer variable
checkUninitVar("void a(char c);\n" // value => error checkUninitVar("void a(char c);\n" // value => error