Fix leakNoVarFunctionCall FP (#4523)

This commit is contained in:
chrchr-github 2022-09-29 21:58:11 +02:00 committed by GitHub
parent 5f79f0ccad
commit 6142ba542a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 3 deletions

View File

@ -4976,7 +4976,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun
</function>
<!-- char *strncpy(char *s, const char *ct, size_t n); -->
<function name="strncpy,std::strncpy">
<returnValue type="char *"/>
<returnValue type="char *">arg1</returnValue>
<noreturn>false</noreturn>
<leak-ignore/>
<not-overlapping-data ptr1-arg="1" ptr2-arg="2" size-arg="3"/>

View File

@ -997,9 +997,9 @@ void CheckMemoryLeakNoVar::checkForUnreleasedInputArgument(const Scope *scope)
// check if the output of the function is assigned
const Token* tok2 = tok->next()->astParent();
while (tok2 && tok2->isCast())
while (tok2 && (tok2->isCast() || Token::Match(tok2, "?|:")))
tok2 = tok2->astParent();
if (Token::Match(tok2, "%assign%"))
if (Token::Match(tok2, "%assign%")) // TODO: check if function returns allocated resource
continue;
if (Token::simpleMatch(tok->astTop(), "return"))
continue;

View File

@ -2446,6 +2446,12 @@ private:
" u.reset(strcpy(new char[n], s.c_str()));\n"
"};\n");
ASSERT_EQUALS("", errout.str());
check("struct S { char* p; };\n"
"void f(S* s, int N) {\n"
" s->p = s->p ? strcpy(new char[N], s->p) : nullptr;\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void missingAssignment() {