Skip to content

Commit 5c59d86

Browse files
Fix #12403 FP mallocOnClassWarning for RAII class (#5930)
1 parent 47c3ad1 commit 5c59d86

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

lib/checkclass.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,8 +1429,12 @@ void CheckClass::checkMemset()
14291429
const std::set<const Scope *> parsedTypes;
14301430
checkMemsetType(scope, tok, type, false, parsedTypes);
14311431
}
1432-
} else if (tok->variable() && tok->variable()->typeScope() && Token::Match(tok, "%var% = %name% (") &&
1433-
(mSettings->library.getAllocFuncInfo(tok->tokAt(2)) || mSettings->library.getReallocFuncInfo(tok->tokAt(2)))) {
1432+
} else if (tok->variable() && tok->variable()->typeScope() && Token::Match(tok, "%var% = %name% (")) {
1433+
const Library::AllocFunc* alloc = mSettings->library.getAllocFuncInfo(tok->tokAt(2));
1434+
if (!alloc)
1435+
alloc = mSettings->library.getReallocFuncInfo(tok->tokAt(2));
1436+
if (!alloc || alloc->bufferSize == Library::AllocFunc::BufferSize::none)
1437+
continue;
14341438
const std::set<const Scope *> parsedTypes;
14351439
checkMemsetType(scope, tok->tokAt(2), tok->variable()->typeScope(), true, parsedTypes);
14361440

test/testclass.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2955,7 +2955,7 @@ class TestClass : public TestFixture {
29552955

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

@@ -3587,6 +3587,17 @@ class TestClass : public TestFixture {
35873587
" p = malloc(sizeof(C));\n"
35883588
"}");
35893589
ASSERT_EQUALS("", errout.str());
3590+
3591+
checkNoMemset("class AutoCloseFD {\n"
3592+
" int fd;\n"
3593+
"public:\n"
3594+
" AutoCloseFD(int fd);\n"
3595+
" ~AutoCloseFD();\n"
3596+
"};\n"
3597+
"void f() {\n"
3598+
" AutoCloseFD fd = open(\"abc\", O_RDONLY | O_CLOEXEC);\n"
3599+
"}");
3600+
ASSERT_EQUALS("", errout.str());
35903601
}
35913602

35923603
#define checkThisSubtraction(code) checkThisSubtraction_(code, __FILE__, __LINE__)

0 commit comments

Comments
 (0)