Add null pointer check to fix SEGFAULT (#1499)

* Add null pointer check to fix segfault

* Add first test case to reproduce problem
This commit is contained in:
Igor 2018-12-02 16:36:01 +03:00 committed by Daniel Marjamäki
parent 3e1b34dd8f
commit 61878c5e11
2 changed files with 15 additions and 1 deletions

View File

@ -2964,7 +2964,7 @@ static void valueFlowAfterMove(TokenList *tokenlist, SymbolDatabase* symboldatab
(parent->str() == "return" || // MOVED in return statement (parent->str() == "return" || // MOVED in return statement
parent->str() == "(")) // MOVED in self assignment, isOpenParenthesisMemberFunctionCallOfVarId == true parent->str() == "(")) // MOVED in self assignment, isOpenParenthesisMemberFunctionCallOfVarId == true
continue; continue;
if (parent && parent->astOperand1()->varId() == varId) if (parent && parent->astOperand1() && parent->astOperand1()->varId() == varId)
continue; continue;
const Variable *var = varTok->variable(); const Variable *var = varTok->variable();
if (!var) if (!var)

View File

@ -495,6 +495,20 @@ private:
" return h(std::move(x));\n" " return h(std::move(x));\n"
"}"; "}";
ASSERT_EQUALS(false, testValueOfX(code, 5U, ValueFlow::Value::MovedVariable)); ASSERT_EQUALS(false, testValueOfX(code, 5U, ValueFlow::Value::MovedVariable));
code = "struct X {\n"
"};\n"
"struct Data {\n"
" template<typename Fun>\n"
" void foo(Fun f) {}\n"
"};\n"
"Data g(X value) { return Data(); }\n"
"void f() {\n"
" X x;\n"
" g(std::move(x)).foo([=](int value) mutable {;});\n"
" X y=x;\n"
"}";
TODO_ASSERT_EQUALS(true, false, testValueOfX(code, 11U, ValueFlow::Value::MovedVariable));
} }
void valueFlowCalculations() { void valueFlowCalculations() {