Fix #10513: vector constructed from pointers (#3473)

This commit is contained in:
chrchr-github 2021-10-04 07:55:17 +02:00 committed by GitHub
parent 7621fee3d4
commit 428144c53c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 1 deletions

View File

@ -2356,6 +2356,11 @@ int getArgumentPos(const Variable* var, const Function* f)
return std::distance(f->argumentList.begin(), arg_it);
}
bool isIteratorPair(std::vector<const Token*> args)
{
return args.size() == 2 && ((astIsIterator(args[0]) && astIsIterator(args[1])) || (astIsPointer(args[0]) && astIsPointer(args[1])));
}
const Token *findLambdaStartToken(const Token *last)
{
if (!last || last->str() != "}")

View File

@ -274,6 +274,11 @@ std::vector<const Token *> getArguments(const Token *ftok);
int getArgumentPos(const Variable* var, const Function* f);
/**
* Are the arguments a pair of iterators/pointers?
*/
bool isIteratorPair(std::vector<const Token*> args);
const Token *findLambdaStartToken(const Token *last);
/**

View File

@ -6972,7 +6972,7 @@ static std::vector<ValueFlow::Value> getInitListSize(const Token* tok,
}
return {};
} else if ((args.size() == 1 && astIsContainer(args[0]) && args[0]->valueType()->container == container) ||
(args.size() == 2 && astIsIterator(args[0]) && astIsIterator(args[1]))) {
isIteratorPair(args)) {
return getContainerValues(args[0]);
}
return {makeContainerSizeValue(args.size(), known)};

View File

@ -5177,6 +5177,13 @@ private:
"}";
ASSERT(tokenValues(code, "s . size").empty());
code = "void f() {\n"
" const uint8_t data[] = { 1, 2, 3 };\n"
" std::vector<uint8_t> v{ data, data + sizeof(data) };\n"
" v.size();\n"
"}";
TODO_ASSERT_EQUALS("", "ContainerSizeValue", isKnownContainerSizeValue(tokenValues(code, "v . size"), 3)); // TODO: extract container size
// valueFlowContainerForward, loop
code = "void f() {\n"
" std::stack<Token *> links;\n"