Fix 11512: FN containerOutOfBounds when empty vector is returned (#4928)
This commit is contained in:
parent
2359b9ff82
commit
634f5e254f
|
@ -8507,6 +8507,21 @@ static void valueFlowContainerSize(TokenList* tokenlist,
|
||||||
value.setKnown();
|
value.setKnown();
|
||||||
valueFlowForward(containerTok->next(), containerTok, value, tokenlist, settings);
|
valueFlowForward(containerTok->next(), containerTok, value, tokenlist, settings);
|
||||||
}
|
}
|
||||||
|
} else if (Token::Match(tok->previous(), ">|return (|{") && astIsContainer(tok)) {
|
||||||
|
std::vector<ValueFlow::Value> values;
|
||||||
|
if (Token::simpleMatch(tok, "{")) {
|
||||||
|
values = getInitListSize(tok, tok->valueType(), settings, true);
|
||||||
|
ValueFlow::Value value;
|
||||||
|
value.valueType = ValueFlow::Value::ValueType::TOK;
|
||||||
|
value.tokvalue = tok;
|
||||||
|
value.setKnown();
|
||||||
|
values.push_back(value);
|
||||||
|
} else if (Token::simpleMatch(tok, "(")) {
|
||||||
|
const Token* constructorArgs = tok;
|
||||||
|
values = getContainerSizeFromConstructor(constructorArgs, tok->valueType(), settings, true);
|
||||||
|
}
|
||||||
|
for (const ValueFlow::Value& value : values)
|
||||||
|
setTokenValue(tok, value, settings);
|
||||||
} else if (Token::Match(tok, "%name%|;|{|}|> %var% = {") && Token::simpleMatch(tok->linkAt(3), "} ;")) {
|
} else if (Token::Match(tok, "%name%|;|{|}|> %var% = {") && Token::simpleMatch(tok->linkAt(3), "} ;")) {
|
||||||
Token* containerTok = tok->next();
|
Token* containerTok = tok->next();
|
||||||
if (containerTok->exprId() == 0)
|
if (containerTok->exprId() == 0)
|
||||||
|
|
|
@ -6481,6 +6481,15 @@ private:
|
||||||
" return x;\n"
|
" return x;\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
ASSERT_EQUALS(true, testValueOfXKnown(code, 4U, 0));
|
ASSERT_EQUALS(true, testValueOfXKnown(code, 4U, 0));
|
||||||
|
|
||||||
|
code = "std::vector<int> f() { return std::vector<int>(); }";
|
||||||
|
ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "( ) ;"), 0));
|
||||||
|
|
||||||
|
code = "std::vector<int> f() { return std::vector<int>{}; }";
|
||||||
|
TODO_ASSERT_EQUALS("", "values.size():0", isKnownContainerSizeValue(tokenValues(code, "{ } ;"), 0));
|
||||||
|
|
||||||
|
code = "std::vector<int> f() { return {}; }";
|
||||||
|
ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "{ } ;"), 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
void valueFlowContainerElement()
|
void valueFlowContainerElement()
|
||||||
|
|
Loading…
Reference in New Issue