Fix #9653 FP leakReturnValNotUsed although (void) is specified (#4431)

This commit is contained in:
chrchr-github 2022-09-27 20:09:04 +02:00 committed by GitHub
parent d6f1d7bb23
commit 56e2af5dec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 30 additions and 20 deletions

View File

@ -268,9 +268,9 @@ void CheckFunctions::checkIgnoredReturnValue()
if ((!tok->function() || !Token::Match(tok->function()->retDef, "void %name%")) &&
!WRONG_DATA(!tok->next()->astOperand1(), tok)) {
const Library::UseRetValType retvalTy = mSettings->library.getUseRetValType(tok);
if (mSettings->severity.isEnabled(Severity::warning) &&
((retvalTy == Library::UseRetValType::DEFAULT) ||
(tok->function() && tok->function()->isAttributeNodiscard())))
const bool warn = (tok->function() && tok->function()->isAttributeNodiscard()) || // avoid duplicate warnings for resource-allocating functions
(retvalTy == Library::UseRetValType::DEFAULT && mSettings->library.getAllocFuncInfo(tok) == nullptr);
if (mSettings->severity.isEnabled(Severity::warning) && warn)
ignoredReturnValueError(tok, tok->next()->astOperand1()->expressionString());
else if (mSettings->severity.isEnabled(Severity::style) &&
retvalTy == Library::UseRetValType::ERROR_CODE)

View File

@ -274,6 +274,16 @@ bool CheckMemoryLeak::isReopenStandardStream(const Token *tok) const
return false;
}
bool CheckMemoryLeak::isOpenDevNull(const Token *tok) const
{
if (mSettings_->posix() && tok->str() == "open" && numberOfArguments(tok) == 2) {
const Token* arg = getArguments(tok).at(0);
if (Token::simpleMatch(arg, "\"/dev/null\""))
return true;
}
return false;
}
//--------------------------------------------------------------------------
@ -1053,10 +1063,12 @@ void CheckMemoryLeakNoVar::checkForUnusedReturnValue(const Scope *scope)
if (isReopenStandardStream(tok))
continue;
if (isOpenDevNull(tok))
continue;
// get ast parent, skip casts
const Token *parent = isNew ? tok->astParent() : tok->next()->astParent();
while (parent && parent->str() == "(" && !parent->astOperand2())
while (parent && parent->isCast())
parent = parent->astParent();
bool warn = true;

View File

@ -118,6 +118,11 @@ public:
* @param tok token to check
*/
bool isReopenStandardStream(const Token *tok) const;
/**
* Check if token opens /dev/null
* @param tok token to check
*/
bool isOpenDevNull(const Token *tok) const;
/**
* Report that there is a memory leak (new/malloc/etc)
* @param tok token where memory is leaked

View File

@ -73,7 +73,6 @@ void validCode(int argInt, GHashTableIter * hash_table_iter, GHashTable * hash_t
void g_malloc_test()
{
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress leakReturnValNotUsed
g_malloc(8);
@ -86,7 +85,6 @@ void g_malloc_test()
void g_malloc0_test()
{
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress leakReturnValNotUsed
g_malloc0(8);
@ -99,7 +97,6 @@ void g_malloc0_test()
void g_malloc_n_test()
{
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress leakReturnValNotUsed
g_malloc_n(8, 1);
@ -112,7 +109,6 @@ void g_malloc_n_test()
void g_malloc0_n_test()
{
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress leakReturnValNotUsed
g_malloc0_n(8, 1);
@ -125,7 +121,6 @@ void g_malloc0_n_test()
void g_try_malloc_test()
{
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress leakReturnValNotUsed
g_try_malloc(8);
@ -138,7 +133,6 @@ void g_try_malloc_test()
void g_try_malloc0_test()
{
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress leakReturnValNotUsed
g_try_malloc0(8);
@ -151,7 +145,6 @@ void g_try_malloc0_test()
void g_try_malloc_n_test()
{
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress leakReturnValNotUsed
g_try_malloc_n(8, 1);
@ -164,7 +157,6 @@ void g_try_malloc_n_test()
void g_try_malloc0_n_test()
{
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress leakReturnValNotUsed
g_try_malloc0_n(8, 1);
@ -374,7 +366,6 @@ void g_error_new_test()
printf("%p", pNew1);
g_error_free(pNew1);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress leakReturnValNotUsed
g_error_new(1, -2, "a %d", 1);

View File

@ -838,7 +838,6 @@ void nullPointer(char *p, int fd, pthread_mutex_t mutex)
// cppcheck-suppress unreadVariable
// cppcheck-suppress nullPointer
int ret = access(NULL, 0);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress leakReturnValNotUsed
// cppcheck-suppress nullPointer
fdopen(fd, NULL);
@ -978,7 +977,6 @@ void noleak(int x, int y, int z)
void ignoredReturnValue(void *addr, int fd)
{
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress leakReturnValNotUsed
mmap(addr, 255, PROT_NONE, MAP_PRIVATE, fd, 0);
// cppcheck-suppress ignoredReturnValue
@ -1065,7 +1063,6 @@ void uninitvar(int fd)
// cppcheck-suppress uninitvar
int access_ret = access("file", x3);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress leakReturnValNotUsed
// cppcheck-suppress uninitvar
fdopen(x4, "rw");

View File

@ -287,7 +287,6 @@ void pointerLessThanZero_aligned_alloc(void)
void unusedRetVal_aligned_alloc(void)
{
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress leakReturnValNotUsed
aligned_alloc(8, 16);
}

View File

@ -681,7 +681,6 @@ void ignoredReturnValue()
// cppcheck-suppress leakReturnValNotUsed
CreateEventEx(NULL, L"test", CREATE_EVENT_INITIAL_SET, EVENT_MODIFY_STATE);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress leakReturnValNotUsed
_malloca(10);
// cppcheck-suppress ignoredReturnValue
@ -693,10 +692,8 @@ void ignoredReturnValue()
// cppcheck-suppress ignoredReturnValue
GetProcessHeap();
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress leakReturnValNotUsed
HeapAlloc(GetProcessHeap(), 0, 10);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress leakReturnValNotUsed
HeapReAlloc(GetProcessHeap(), 0, 1, 0);

View File

@ -2311,6 +2311,7 @@ private:
TEST_CASE(getAllocationType);
TEST_CASE(crash1); // #10729
TEST_CASE(openDevNull); // #9653
}
void functionParameter() {
@ -2852,6 +2853,14 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
}
void openDevNull() {
check("void f() {\n" // #9653
" (void)open(\"/dev/null\", O_RDONLY);\n"
" open(\"/dev/null\", O_WRONLY);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
};
REGISTER_TEST(TestMemleakNoVar)