Menu

uninitMemberVar false negative when memset() is used

2015-10-19
2015-11-03
  • Cong Van Nguyen

    Cong Van Nguyen - 2015-10-19

    Hi,

    I couldn't log in into cppcheck.net (no verification email received), so just report the following bug here.

    //------------------------------------------------------------------------------------------------------

    include <iostream>

    include <string.h>

    typedef struct
    {
    int f;
    } Foo;

    typedef struct
    {
    int b;
    } Bar;

    class FooBar
    {
    public:
    FooBar();
    Foo foo;
    Bar bar;
    };

    FooBar::FooBar()
    {
    memset(&foo, 0, sizeof(foo));
    }

    int main(void)
    {
    FooBar foobar;
    std::cout << foobar.foo.f << foobar.bar.b << std::endl;
    return 0;
    }

    For the above example code, cppcheck 1.70 correctly reports the warning "[test1.cpp:22]: (warning) Member variable 'FooBar::bar' is not initialized in the constructor". However, if I replace the memset line FooBar constructor by:
    memset(&this->foo, 0, sizeof(this->foo));
    , cppcheck won't report the warning anymore. Look like it has issues with interpreting "this->" here.

    Kind regards,

    Cong-Van

     
  • Daniel Marjamäki

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.