From dc61b6342d2aeffd74bf105331f29354f2c6ed92 Mon Sep 17 00:00:00 2001 From: Simon Martin Date: Sat, 1 Oct 2016 13:46:58 +0200 Subject: [PATCH] Ticket #7068: Treat "memset(&this->member, ..." as member initialization. --- lib/checkclass.cpp | 12 +++++++++++ test/testconstructors.cpp | 42 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 7cc0d6e6b..729295cbd 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -628,6 +628,18 @@ void CheckClass::initializeVarList(const Function &func, std::liststr() == "::") + ftok = ftok->next(); + int offsetToMember = 4; + if (ftok->tokAt(ftok->strAt(2) == "&")) + ++offsetToMember; + assignVar(ftok->tokAt(offsetToMember)->varId(), scope, usage); + ftok = ftok->linkAt(1); + continue; + } + // Clearing array.. else if (Token::Match(ftok, "::| memset ( %name% ,")) { if (ftok->str() == "::") diff --git a/test/testconstructors.cpp b/test/testconstructors.cpp index d7bcdb7b1..c897e1c91 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -2807,6 +2807,48 @@ private: "};"); ASSERT_EQUALS("", errout.str()); + + // Ticket #7068 + check("struct Foo {\n" + " int * p;\n" + " char c;\n" + " Foo() { memset(p, 0, sizeof(int)); }\n" + "};"); + ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'Foo::c' is not initialized in the constructor.\n", errout.str()); + check("struct Foo {\n" + " int i;\n" + " char c;\n" + " Foo() { memset(&i, 0, sizeof(int)); }\n" + "};"); + ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'Foo::c' is not initialized in the constructor.\n", errout.str()); + check("struct Foo { int f; };\n" + "struct Bar { int b; };\n" + "struct FooBar {\n" + " FooBar() {\n" + " memset(&foo, 0, sizeof(foo));\n" + " }\n" + " Foo foo;\n" + " Bar bar;\n" + "};\n" + "int main() {\n" + " FooBar foobar;\n" + " return foobar.foo.f + foobar.bar.b;\n" + "}"); + ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'FooBar::bar' is not initialized in the constructor.\n", errout.str()); + check("struct Foo { int f; };\n" + "struct Bar { int b; };\n" + "struct FooBar {\n" + " FooBar() {\n" + " memset(&this->foo, 0, sizeof(this->foo));\n" + " }\n" + " Foo foo;\n" + " Bar bar;\n" + "};\n" + "int main() {\n" + " FooBar foobar;\n" + " return foobar.foo.f + foobar.bar.b;\n" + "}"); + ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'FooBar::bar' is not initialized in the constructor.\n", errout.str()); } void privateCtor1() {