Skip to content

Commit

Permalink
Fix #12403 FP mallocOnClassWarning for RAII class (#5930)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Jan 31, 2024
1 parent 47c3ad1 commit 5c59d86
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1429,8 +1429,12 @@ void CheckClass::checkMemset()
const std::set<const Scope *> parsedTypes;
checkMemsetType(scope, tok, type, false, parsedTypes);
}
} else if (tok->variable() && tok->variable()->typeScope() && Token::Match(tok, "%var% = %name% (") &&
(mSettings->library.getAllocFuncInfo(tok->tokAt(2)) || mSettings->library.getReallocFuncInfo(tok->tokAt(2)))) {
} else if (tok->variable() && tok->variable()->typeScope() && Token::Match(tok, "%var% = %name% (")) {
const Library::AllocFunc* alloc = mSettings->library.getAllocFuncInfo(tok->tokAt(2));
if (!alloc)
alloc = mSettings->library.getReallocFuncInfo(tok->tokAt(2));
if (!alloc || alloc->bufferSize == Library::AllocFunc::BufferSize::none)
continue;
const std::set<const Scope *> parsedTypes;
checkMemsetType(scope, tok->tokAt(2), tok->variable()->typeScope(), true, parsedTypes);

Expand Down
13 changes: 12 additions & 1 deletion test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2955,7 +2955,7 @@ class TestClass : public TestFixture {

#define checkNoMemset(...) checkNoMemset_(__FILE__, __LINE__, __VA_ARGS__)
void checkNoMemset_(const char* file, int line, const char code[]) {
const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::portability).library("std.cfg").build();
const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::portability).library("std.cfg").library("posix.cfg").build();
checkNoMemset_(file, line, code, settings);
}

Expand Down Expand Up @@ -3587,6 +3587,17 @@ class TestClass : public TestFixture {
" p = malloc(sizeof(C));\n"
"}");
ASSERT_EQUALS("", errout.str());

checkNoMemset("class AutoCloseFD {\n"
" int fd;\n"
"public:\n"
" AutoCloseFD(int fd);\n"
" ~AutoCloseFD();\n"
"};\n"
"void f() {\n"
" AutoCloseFD fd = open(\"abc\", O_RDONLY | O_CLOEXEC);\n"
"}");
ASSERT_EQUALS("", errout.str());
}

#define checkThisSubtraction(code) checkThisSubtraction_(code, __FILE__, __LINE__)
Expand Down

0 comments on commit 5c59d86

Please sign in to comment.