googletest icon indicating copy to clipboard operation
googletest copied to clipboard

[Bug]: `ReturnRef` of type `shared_ptr<Mock<...>> const&` segfaults

Open neugepower opened this issue 1 year ago • 0 comments
trafficstars

Describe the issue

We came across a problem with one of our tests. We have a given interface that has a getter that returns a const& to a shared pointer:

class IDatabase {
public:
    virtual std::shared_ptr<IConnection> const& connection() const = 0;
};

We then defined a mock for this interface type:

class DatabaseMock : public IDatabase {
public:
    MOCK_CONST_METHOD0(connection, std::shared_ptr<IConnection> const&());
};

Lastly, we have defined an expectation on the mocked interface to return another mock:

EXPECT_CALL(*mockDatabase, connection()).WillRepeatedly(ReturnRef(mockConnection));

Given this setup, as soon as we access mockDatabase->connection() the code segfaults. The code does not segfault if instead of returning a mock connection we return a non-mock connection object. Also returning the shared pointer as value instead of const& seems to mitigate the problem (unfortunately we can not change our API easily as it is widely used).

We were wondering if this is a limitation of googletest or a bug. Please see the godbolt link below for the complete and runable example.

Steps to reproduce the problem

https://godbolt.org/z/aKfTecr5v

See last test and commented lines of code. After uncommenting the code the test will segfault.

What version of GoogleTest are you using?

1.10.0 and 1.12.1

What operating system and version are you using?

godbolt & Linux RHEL8

What compiler and version are you using?

gcc 10.5 godbold & gcc version 11.2.1 20220127 (Red Hat 11.2.1-9) (GCC)

What build system are you using?

godbolt & cmake version 3.28.3

Additional context

No response

neugepower avatar Mar 22 '24 18:03 neugepower