Fix FP leakVarNotUsed with freopen() and stdin (#2034)

One usecase for freopen() is to redirect input and output streams to
files. For that, the return value is not needed.
This commit is contained in:
Rikard Falkeborn 2019-07-25 21:09:23 +02:00 committed by Daniel Marjamäki
parent 9009eeb83d
commit a69a570505
2 changed files with 14 additions and 0 deletions

View File

@ -1006,6 +1006,15 @@ void CheckMemoryLeakNoVar::checkForUnusedReturnValue(const Scope *scope)
if (tok != tok->next()->astOperand1())
continue;
if (getReallocationType(tok, 0) == File) {
const Library::AllocFunc *f = mSettings->library.getReallocFuncInfo(tok);
if (f && f->reallocArg > 0 && f->reallocArg <= numberOfArguments(tok)) {
const Token* arg = getArguments(tok).at(f->reallocArg - 1);
if (Token::Match(arg, "stdin|stdout|stderr"))
continue;
}
}
// get ast parent, skip casts
const Token *parent = tok->next()->astParent();
while (parent && parent->str() == "(" && !parent->astOperand2())

View File

@ -2329,6 +2329,11 @@ private:
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function 'freopen' is not stored.\n", errout.str());
check("void foo() {\n"
" freopen(\"file.txt\", \"r\", stdin);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("struct Holder {\n"
" Holder(FILE* f) : file(f) {}\n"
" ~Holder() { fclose(file); }\n"