C6001 with overloaded assignment operator

95 views Asked by At

This simplified example code raises C6001 under static analysis:

struct st_facade
{
    st_facade &operator=(const int new_value) { m_value = new_value; return *this;}

private:
    int m_value;
};

struct foo
{
    st_facade my_int;
};

void main()
{
    foo f;
    f.my_int= 0; // C6001: Using uninitialized memory 'f'.
}

My understanding is that this warning is raised because the overloaded assignment operator hypothetically could be reading uninitialized memory. But in this case, we are not doing that -- we are in fact initializing the memory.

How can I tell the compiler that the assignment operator here is not using any uninitialized memory? I've tried to express this using several different SAL annotations, but nothing has worked.

Zero-initializing the memory (i.e. with foo f{}; or st_facade my_int{};) is not the correct answer in this case. In the actual code, such initialization could have an unacceptable perf impact

[edit] For similar reasons, constructor-based initialization is not the correct answer either. We need this type to be uninitialized on creation; we need to inform the static analyzer that the assignment operator performs the initialization.

[edit 2] More generally, we need a way to tell the static analyzer "this method initializes the object" without implementing a constructor or zero-initializing every instance of the type.

1

There are 1 answers

3
Jacob Schultz Andersen On

That's why C++ have constructors.

struct st_facade
{
    explicit st_facade(int value) 
        : m_value{ value } 
    {}
    
    st_facade &operator=(const int new_value) { m_value = new_value; return *this;}

private:
    int m_value;
};

struct foo
{
    explicit foo(int value)
        : my_int{ value }
    {}

    st_facade my_int;
};

int main()
{
    foo f{0};
}