Skip to content

Commit

Permalink
Fix FN unusedAllocatedMemory (f'up to #12332) (#6158)
Browse files Browse the repository at this point in the history
See  #6154
  • Loading branch information
chrchr-github authored Mar 22, 2024
1 parent 427e8e9 commit 5dcf217
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cfg/opencv2.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<noreturn>false</noreturn>
<returnValue type="void"/>
<arg nr="1">
<not-uninit indirect="1"/>
<not-uninit/>
</arg>
</function>
<!-- void * cv::fastMalloc (size_t bufSize) -->
Expand Down
6 changes: 6 additions & 0 deletions lib/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1075,20 +1075,26 @@ bool Library::isuninitargbad(const Token *ftok, int argnr, int indirect, bool *h
/** get allocation info for function */
const Library::AllocFunc* Library::getAllocFuncInfo(const Token *tok) const
{
while (Token::simpleMatch(tok, "::"))
tok = tok->astOperand2() ? tok->astOperand2() : tok->astOperand1();
const std::string funcname = getFunctionName(tok);
return isNotLibraryFunction(tok) && functions.find(funcname) != functions.end() ? nullptr : getAllocDealloc(mAlloc, funcname);
}

/** get deallocation info for function */
const Library::AllocFunc* Library::getDeallocFuncInfo(const Token *tok) const
{
while (Token::simpleMatch(tok, "::"))
tok = tok->astOperand2() ? tok->astOperand2() : tok->astOperand1();
const std::string funcname = getFunctionName(tok);
return isNotLibraryFunction(tok) && functions.find(funcname) != functions.end() ? nullptr : getAllocDealloc(mDealloc, funcname);
}

/** get reallocation info for function */
const Library::AllocFunc* Library::getReallocFuncInfo(const Token *tok) const
{
while (Token::simpleMatch(tok, "::"))
tok = tok->astOperand2() ? tok->astOperand2() : tok->astOperand1();
const std::string funcname = getFunctionName(tok);
return isNotLibraryFunction(tok) && functions.find(funcname) != functions.end() ? nullptr : getAllocDealloc(mRealloc, funcname);
}
Expand Down
9 changes: 6 additions & 3 deletions test/cfg/opencv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ void validCode(const char* argStr)
cvStr += " World";
std::cout << cvStr;

char * pBuf = (char *)cv::fastMalloc(20); // cppcheck-suppress cstyleCast
// cppcheck-suppress [cstyleCast, unusedAllocatedMemory]
char * pBuf = (char *)cv::fastMalloc(20);
cv::fastFree(pBuf);
}

Expand All @@ -42,7 +43,9 @@ void ignoredReturnValue()

void memleak()
{
const char * pBuf = (char *)cv::fastMalloc(1000); // cppcheck-suppress cstyleCast
std::cout << pBuf; // cppcheck-suppress valueFlowBailoutIncompleteVar
// cppcheck-suppress cstyleCast
const char * pBuf = (char *)cv::fastMalloc(1000);
// cppcheck-suppress [uninitdata, valueFlowBailoutIncompleteVar]
std::cout << pBuf;
// cppcheck-suppress memleak
}
6 changes: 3 additions & 3 deletions test/cfg/std.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2928,7 +2928,7 @@ void uninitvar_longjmp(void)
void uninitvar_malloc(void)
{
size_t size;
// cppcheck-suppress [uninitvar, cstyleCast]
// cppcheck-suppress [uninitvar, cstyleCast, unusedAllocatedMemory]
int *p = (int*)std::malloc(size);
free(p);
}
Expand Down Expand Up @@ -4957,7 +4957,7 @@ void std_vector_data_arithmetic()

void memleak_std_malloc() // #12332
{
//cppcheck-suppress [unreadVariable, constVariablePointer]
//cppcheck-suppress [unreadVariable, constVariablePointer, unusedAllocatedMemory]
void* p = std::malloc(1);
//cppcheck-suppress memleak
}
Expand All @@ -4971,7 +4971,7 @@ void memleak_std_realloc(void* block, size_t newsize)

void unusedAllocatedMemory_std_free()
{
// TODO cppcheck-suppress unusedAllocatedMemory
// cppcheck-suppress unusedAllocatedMemory
void* p = std::malloc(1);
std::free(p);
}
Expand Down

0 comments on commit 5dcf217

Please sign in to comment.