Skip to content

Commit dc61b63

Browse files
committed
Ticket danmar#7068: Treat "memset(&this->member, ..." as member initialization.
1 parent d4d1d32 commit dc61b63

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

lib/checkclass.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,18 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
628628
return;
629629
}
630630

631+
// Ticket #7068
632+
else if (Token::Match(ftok, "::| memset ( &| this . %name%")) {
633+
if (ftok->str() == "::")
634+
ftok = ftok->next();
635+
int offsetToMember = 4;
636+
if (ftok->tokAt(ftok->strAt(2) == "&"))
637+
++offsetToMember;
638+
assignVar(ftok->tokAt(offsetToMember)->varId(), scope, usage);
639+
ftok = ftok->linkAt(1);
640+
continue;
641+
}
642+
631643
// Clearing array..
632644
else if (Token::Match(ftok, "::| memset ( %name% ,")) {
633645
if (ftok->str() == "::")

test/testconstructors.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2807,6 +2807,48 @@ class TestConstructors : public TestFixture {
28072807
"};");
28082808

28092809
ASSERT_EQUALS("", errout.str());
2810+
2811+
// Ticket #7068
2812+
check("struct Foo {\n"
2813+
" int * p;\n"
2814+
" char c;\n"
2815+
" Foo() { memset(p, 0, sizeof(int)); }\n"
2816+
"};");
2817+
ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'Foo::c' is not initialized in the constructor.\n", errout.str());
2818+
check("struct Foo {\n"
2819+
" int i;\n"
2820+
" char c;\n"
2821+
" Foo() { memset(&i, 0, sizeof(int)); }\n"
2822+
"};");
2823+
ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'Foo::c' is not initialized in the constructor.\n", errout.str());
2824+
check("struct Foo { int f; };\n"
2825+
"struct Bar { int b; };\n"
2826+
"struct FooBar {\n"
2827+
" FooBar() {\n"
2828+
" memset(&foo, 0, sizeof(foo));\n"
2829+
" }\n"
2830+
" Foo foo;\n"
2831+
" Bar bar;\n"
2832+
"};\n"
2833+
"int main() {\n"
2834+
" FooBar foobar;\n"
2835+
" return foobar.foo.f + foobar.bar.b;\n"
2836+
"}");
2837+
ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'FooBar::bar' is not initialized in the constructor.\n", errout.str());
2838+
check("struct Foo { int f; };\n"
2839+
"struct Bar { int b; };\n"
2840+
"struct FooBar {\n"
2841+
" FooBar() {\n"
2842+
" memset(&this->foo, 0, sizeof(this->foo));\n"
2843+
" }\n"
2844+
" Foo foo;\n"
2845+
" Bar bar;\n"
2846+
"};\n"
2847+
"int main() {\n"
2848+
" FooBar foobar;\n"
2849+
" return foobar.foo.f + foobar.bar.b;\n"
2850+
"}");
2851+
ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'FooBar::bar' is not initialized in the constructor.\n", errout.str());
28102852
}
28112853

28122854
void privateCtor1() {

0 commit comments

Comments
 (0)