mgehre / llvm-project

The home of the clang-based implementation of lifetime safety warnings.
39 stars 4 forks source link

False positive when returning pointer to member #90

Closed jerryct closed 4 years ago

jerryct commented 4 years ago

The following code

struct A
{
    const char* Get(int i) const {
        if (i < 0)
            return &a;
        else if (i == 0)
            return &b;
        else
            return &c;
    }

    char a{};
    char b{};
    char c{};
};

produces warnings about potential nullptr.

<source>:9:13: warning: returning a pointer with points-to set ((*this).c) where points-to set ((null)) is expected [-Wlifetime]
            return &c;
            ^~~~~~~~~

<source>:7:13: warning: returning a pointer with points-to set ((*this).b) where points-to set ((null)) is expected [-Wlifetime]
            return &b;
            ^~~~~~~~~

<source>:5:13: warning: returning a pointer with points-to set ((*this).a) where points-to set ((null)) is expected [-Wlifetime]
            return &a;
            ^~~~~~~~~

But the returned pointer is always backed by a non-reference and non-pointer member.

Xazax-hun commented 4 years ago

Here the problem is that we do not correctly deduce the lifetime postcondition for the Get method. Thanks for reporting this, I will look into that once I have some time!

Xazax-hun commented 4 years ago

This should be fixed by https://github.com/mgehre/llvm-project/commit/51afbcfafa64f992b8ee212bfa68ca98e02dc7a7