google / google/googletest

Matching std::reference_wrapper using RefMatcher yields undefined behavior

Open
#2,022 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
39.5k
Forks
10.9k
Avg merge
6d 13h
Merged PRs (30d)
1

Description

The test below yields undefined behavior or access violations using VS2017 Version 15.9.2.

It seems that the ref matcher tries to compare addresses of the std::reference_wrapper objects themselves instead of comparing the adresses of the objects referenced by the std::reference_wrapper.

```
struct Object
{
int id;
};

std::ostream & operator<<(std::ostream & strm, Object const & object)
{
return strm << "of type Object with id " << object.id;
}

struct Dummy
{
explicit Dummy(int i)
: object{i}
{}

Object object;
};

TEST(Reference_Wrapper, MatcherBug)
{
Dummy dummy1{1};
Dummy dummy2{2};
Dummy dummy3{3};
std::vector> objects{ dummy1.object, dummy2.object, dummy3.object };
EXPECT_THAT(objects[0], Ref(dummy1.object));
EXPECT_THAT(objects[1], Ref(dummy2.object));
EXPECT_THAT(objects[2], Ref(dummy3.object));
EXPECT_THAT(objects, ElementsAre(Ref(dummy1.object), Ref(dummy2.object), Ref(dummy3.object)));
};
```

Contributor guide

Open the contributing guide

Research direction

Start with the provided Reference_Wrapper.MatcherBug test and reproduce it using VS2017 Version 15.9.2, focusing on Ref with std::reference_wrapper and ElementsAre. Done means the test no longer has undefined behavior or access violations and compares the referenced Object instances correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.