Are temporary values handled differently in a C++ coroutine?

Viewed 62

I'm trying to use C++ coroutine and I'm running on mysterious use after free bug

Here is link to the complete code with all the coroutine boilerplate in godbolt: https://godbolt.org/z/4shK19Eea

This are included bellow:

Compiler flags are: -ggdb3 -Og -std=c++20 -Wall -Wextra -Werror -D_GLIBCXX_DEBUG -fsanitize=address

I tried g++-11.2 g++-12.0.1 and g++-12.1 but got the same result

However, clang++-14.0 is perfectly happy with this code

Any clues to what is happening?

using namespace std;

struct S {
 public:
  string s;

  int size() const { return s.size(); }
};

Task<int> str_size(const S v) {
  cout << v.s << endl;
  co_return v.size();
}

// This is fine

Task<int> test1() {
  auto s = S{.s = "foo"};
  auto p = co_await str_size(s);

  co_return p;
}

// This is fine too

Task<int> test2() {
  auto t =  str_size(S{.s = "foo"});
  auto p = co_await t;

  co_return p;
}

// This is not, address sanitizer claims this code causes a use after free

Task<int> test3() {
  auto p = co_await str_size(S{.s = "foo"});

  co_return p;
}
0 Answers
Related