Skip to content

Commit 7711882

Browse files
authored
Fix 10574: ValueFlow: conditional values in constructor initializer list (#3556)
1 parent c057dcc commit 7711882

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

lib/reverseanalyzer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ struct ReverseTraversal {
127127
i = tok->index();
128128
if (tok == start || (tok->str() == "{" && (tok->scope()->type == Scope::ScopeType::eFunction ||
129129
tok->scope()->type == Scope::ScopeType::eLambda))) {
130+
const Function* f = tok->scope()->function;
131+
if (f && f->isConstructor()) {
132+
if (const Token* initList = f->constructorMemberInitialization())
133+
traverse(tok->previous(), tok->tokAt(initList->index() - tok->index()));
134+
}
130135
break;
131136
}
132137
if (Token::Match(tok, "return|break|continue"))

test/testvalueflow.cpp

100644100755
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class TestValueFlow : public TestFixture {
8181
TEST_CASE(valueFlowBeforeConditionSwitch);
8282
TEST_CASE(valueFlowBeforeConditionTernaryOp);
8383
TEST_CASE(valueFlowBeforeConditionForward);
84+
TEST_CASE(valueFlowBeforeConditionConstructor);
8485

8586
TEST_CASE(valueFlowAfterAssign);
8687
TEST_CASE(valueFlowAfterSwap);
@@ -1753,6 +1754,30 @@ class TestValueFlow : public TestFixture {
17531754
ASSERT_EQUALS(true, testValueOfX(code, 4U, 123));
17541755
}
17551756

1757+
void valueFlowBeforeConditionConstructor()
1758+
{
1759+
const char* code;
1760+
1761+
code = "struct Fred {\n"
1762+
" Fred(int *x)\n"
1763+
" : i(*x) {\n" // <- dereference x
1764+
" if (!x) {}\n" // <- check x
1765+
" }\n"
1766+
" int i;\n"
1767+
"};\n";
1768+
ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));
1769+
1770+
code = "struct Fred {\n"
1771+
" Fred(int *x)\n"
1772+
" : i(*x), j(0) {\n" // <- dereference x
1773+
" if (!x) {}\n" // <- check x
1774+
" }\n"
1775+
" int i;\n"
1776+
" int j;\n"
1777+
"};\n";
1778+
ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));
1779+
}
1780+
17561781
void valueFlowAfterAssign() {
17571782
const char *code;
17581783

0 commit comments

Comments
 (0)