https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108643
Bug ID: 108643
Summary: Initializing parameter by ref in coroutine function
causes memory corruption
Product: gcc
Version: 12.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: menkaur at gmail dot com
Target Milestone: ---
Created attachment 54392
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54392&action=edit
example code
I'm using libcoro (https://github.com/jbaldwin/libcoro) for generator class, on
my system it's compiled and installed
To reproduce bug, you have to write coroutine as follows:
coro::generator<const int> vector_to_generator(const std::vector<int> &test)
{
for (auto &i : test)
{
co_yield i;
}
}
test method looks like this:
coro::generator<const int> test(coro::generator<const int> source, const
std::string &prompt)
{
for (auto &i : source)
{
std::cout << prompt << i << std::endl;
co_yield i;
}
}
if we initialize test with initializer list instead of another object, this
will result in memory corruption:
for (auto &i : test(vector_to_generator({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
"test:"))
{
}
expected output:
test:1671954983
test:5
test:-724690709
test:621133568
test:5
test:6
test:7
test:8
test:9
test:10
(or segfault, which is how I found it)