Fix 10574: ValueFlow: conditional values in constructor initializer list (#3556)

This commit is contained in:
Paul Fultz II 2021-11-11 01:01:10 -06:00 committed by GitHub
parent c057dcce0f
commit 771188238c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -127,6 +127,11 @@ struct ReverseTraversal {
i = tok->index();
if (tok == start || (tok->str() == "{" && (tok->scope()->type == Scope::ScopeType::eFunction ||
tok->scope()->type == Scope::ScopeType::eLambda))) {
const Function* f = tok->scope()->function;
if (f && f->isConstructor()) {
if (const Token* initList = f->constructorMemberInitialization())
traverse(tok->previous(), tok->tokAt(initList->index() - tok->index()));
}
break;
}
if (Token::Match(tok, "return|break|continue"))

25
test/testvalueflow.cpp Normal file → Executable file
View File

@ -81,6 +81,7 @@ private:
TEST_CASE(valueFlowBeforeConditionSwitch);
TEST_CASE(valueFlowBeforeConditionTernaryOp);
TEST_CASE(valueFlowBeforeConditionForward);
TEST_CASE(valueFlowBeforeConditionConstructor);
TEST_CASE(valueFlowAfterAssign);
TEST_CASE(valueFlowAfterSwap);
@ -1753,6 +1754,30 @@ private:
ASSERT_EQUALS(true, testValueOfX(code, 4U, 123));
}
void valueFlowBeforeConditionConstructor()
{
const char* code;
code = "struct Fred {\n"
" Fred(int *x)\n"
" : i(*x) {\n" // <- dereference x
" if (!x) {}\n" // <- check x
" }\n"
" int i;\n"
"};\n";
ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));
code = "struct Fred {\n"
" Fred(int *x)\n"
" : i(*x), j(0) {\n" // <- dereference x
" if (!x) {}\n" // <- check x
" }\n"
" int i;\n"
" int j;\n"
"};\n";
ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));
}
void valueFlowAfterAssign() {
const char *code;