Fix issue 9551: Out-of-bounds in getLifetimeTokens() (#2461)

This commit is contained in:
Paul Fultz II 2019-12-29 01:23:58 -06:00 committed by Daniel Marjamäki
parent ad352838c0
commit 75de485c4d
2 changed files with 15 additions and 1 deletions

View File

@ -3199,7 +3199,11 @@ std::vector<LifetimeToken> getLifetimeTokens(const Token* tok, ValueFlow::Value:
int n = getArgumentPos(argvar, f);
if (n < 0)
return std::vector<LifetimeToken> {};
const Token* argTok = getArguments(tok->previous()).at(n);
std::vector<const Token*> args = getArguments(tok->previous());
// TODO: Track lifetimes of default parameters
if (n >= args.size())
return std::vector<LifetimeToken> {};
const Token* argTok = args[n];
lt.errorPath.emplace_back(returnTok, "Return reference.");
lt.errorPath.emplace_back(tok->previous(), "Called function passing '" + argTok->str() + "'.");
std::vector<LifetimeToken> arglts = LifetimeToken::setInconclusive(

View File

@ -4387,6 +4387,16 @@ private:
" if (0 * (x > 2)) {}\n"
"}\n";
valueOfTok(code, "x");
code = "const int& f(int, const int& y = 0);\n"
"const int& f(int, const int& y) {\n"
" return y;\n"
"}\n"
"const int& g(int x) {\n"
" const int& r = f(x);\n"
" return r;\n"
"}\n";
valueOfTok(code, "0");
}
};