Fix crash in getInitListSize (#2960)

This commit is contained in:
Paul Fultz II 2020-12-19 05:23:19 -06:00 committed by GitHub
parent d591b06ba7
commit a770342593
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 4 deletions

View File

@ -5978,10 +5978,10 @@ static void valueFlowIteratorInfer(TokenList *tokenlist, const Settings *setting
} }
} }
static std::vector<ValueFlow::Value> getInitListSize(const Token* tok) static std::vector<ValueFlow::Value> getInitListSize(const Token* tok, const Library::Container *container)
{ {
std::vector<const Token*> args = getArguments(tok); std::vector<const Token*> args = getArguments(tok);
if ((args.size() == 1 && astIsContainer(args[0]) && args[0]->valueType()->container == tok->valueType()->container) || if ((args.size() == 1 && astIsContainer(args[0]) && args[0]->valueType()->container == container) ||
(args.size() == 2 && astIsIterator(args[0]) && astIsIterator(args[1]))) { (args.size() == 2 && astIsIterator(args[0]) && astIsIterator(args[1]))) {
std::vector<ValueFlow::Value> values; std::vector<ValueFlow::Value> values;
std::copy_if(args[0]->values().begin(), std::copy_if(args[0]->values().begin(),
@ -6021,7 +6021,7 @@ static void valueFlowContainerSize(TokenList *tokenlist, SymbolDatabase* symbold
continue; continue;
} else if (Token::simpleMatch(var->nameToken()->next(), "{")) { } else if (Token::simpleMatch(var->nameToken()->next(), "{")) {
const Token* initList = var->nameToken()->next(); const Token* initList = var->nameToken()->next();
values = getInitListSize(initList); values = getInitListSize(initList, var->valueType()->container);
} }
for (const ValueFlow::Value& value : values) for (const ValueFlow::Value& value : values)
valueFlowContainerForward(var->nameToken()->next(), var, value, tokenlist); valueFlowContainerForward(var->nameToken()->next(), var, value, tokenlist);
@ -6041,7 +6041,7 @@ static void valueFlowContainerSize(TokenList *tokenlist, SymbolDatabase* symbold
} else if (Token::Match(tok, "%name%|;|{|}|> %var% = {") && Token::simpleMatch(tok->linkAt(3), "} ;")) { } else if (Token::Match(tok, "%name%|;|{|}|> %var% = {") && Token::simpleMatch(tok->linkAt(3), "} ;")) {
const Token* containerTok = tok->next(); const Token* containerTok = tok->next();
if (astIsContainer(containerTok)) { if (astIsContainer(containerTok)) {
std::vector<ValueFlow::Value> values = getInitListSize(tok->tokAt(3)); std::vector<ValueFlow::Value> values = getInitListSize(tok->tokAt(3), containerTok->valueType()->container);
for (const ValueFlow::Value& value : values) for (const ValueFlow::Value& value : values)
valueFlowContainerForward(containerTok->next(), containerTok->variable(), value, tokenlist); valueFlowContainerForward(containerTok->next(), containerTok->variable(), value, tokenlist);
} }

View File

@ -4968,6 +4968,16 @@ private:
" }\n" " }\n"
"}\n"; "}\n";
valueOfTok(code, "a"); valueOfTok(code, "a");
code = "class A{\n"
" void f() {\n"
" std::string c{s()};\n"
" }\n"
" std::string s() {\n"
" return \"\";\n"
" }\n"
"};\n";
valueOfTok(code, "c");
} }
void valueFlowHang() { void valueFlowHang() {