#7257 segmentation fault in valueFlowSwitchVariable (invalid code). Correct SymbolDatabase::validateVariables() so it does not complain about function arguments for function without body

This commit is contained in:
Alexander Mai 2016-02-03 21:52:02 +01:00
parent bd61b9e7b7
commit cfe9c01bf8
3 changed files with 34 additions and 3 deletions

View File

@ -1390,11 +1390,36 @@ void SymbolDatabase::validateExecutableScopes() const
} }
} }
namespace {
const Function* getFunctionForArgumentvariable(const Variable * const var, const std::vector<const Scope *>& functionScopes)
{
const std::size_t functions = functionScopes.size();
for (std::size_t i = 0; i < functions; ++i) {
const Scope* const scope = functionScopes[i];
const Function* const function = scope->function;
if (function) {
for (std::size_t arg=0; arg < function->argCount(); ++arg) {
if (var==function->getArgumentVar(arg))
return function;
}
}
}
return nullptr;
}
}
void SymbolDatabase::validateVariables() const void SymbolDatabase::validateVariables() const
{ {
for (std::vector<const Variable *>::const_iterator iter = _variableList.begin(); iter!=_variableList.end(); ++iter) { for (std::vector<const Variable *>::const_iterator iter = _variableList.begin(); iter!=_variableList.end(); ++iter) {
if (*iter && !(*iter)->scope()) { if (*iter) {
throw InternalError((*iter)->nameToken(), "Analysis failed (variable without scope). If the code is valid then please report this failure.", InternalError::INTERNAL); const Variable * const var = *iter;
if (!var->scope()) {
const Function* function = getFunctionForArgumentvariable(var, functionScopes);
if (!var->isArgument() || (function && function->hasBody())) {
throw InternalError(var->nameToken(), "Analysis failed (variable without scope). If the code is valid then please report this failure.", InternalError::INTERNAL);
//std::cout << "!!!Variable found without scope: " << var->nameToken()->str() << std::endl;
}
}
} }
} }
} }

View File

@ -2206,6 +2206,7 @@ static void valueFlowSwitchVariable(TokenList *tokenlist, SymbolDatabase* symbol
errorLogger, errorLogger,
settings); settings);
} }
if (vartok->variable()->scope()) // #7257
valueFlowForward(tok, vartok->variable()->scope()->classEnd, vartok->variable(), vartok->varId(), values, false, tokenlist, errorLogger, settings); valueFlowForward(tok, vartok->variable()->scope()->classEnd, vartok->variable(), vartok->varId(), values, false, tokenlist, errorLogger, settings);
} }
} }

View File

@ -224,6 +224,7 @@ private:
TEST_CASE(garbageCode173); // #6781 TEST_CASE(garbageCode173); // #6781
TEST_CASE(garbageCode174); // #7356 TEST_CASE(garbageCode174); // #7356
TEST_CASE(garbageCode175); TEST_CASE(garbageCode175);
TEST_CASE(garbageCode176);
TEST_CASE(garbageValueFlow); TEST_CASE(garbageValueFlow);
TEST_CASE(garbageSymbolDatabase); TEST_CASE(garbageSymbolDatabase);
TEST_CASE(garbageAST); TEST_CASE(garbageAST);
@ -1474,6 +1475,10 @@ private:
" return t1 ,\n" " return t1 ,\n"
"}"), InternalError); "}"), InternalError);
} }
void garbageCode176() { // #7527
checkCode("class t { { struct } enum class f : unsigned { q } b ; operator= ( T ) { switch ( b ) { case f::q: } } { assert ( b ) ; } } { ; & ( t ) ( f::t ) ; } ;");
}
}; };
REGISTER_TEST(TestGarbage) REGISTER_TEST(TestGarbage)