Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #12403 FP mallocOnClassWarning for RAII class #5930

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading