Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

for loop condition variable destroyed twice by 'continue' #48554

Open Quuxplusone opened 3 years ago

Quuxplusone commented 3 years ago
Bugzilla Link PR49585
Status CONFIRMED
Importance P enhancement
Reported by Aleksander Miera (ammiera@hotmail.com)
Reported on 2021-03-14 06:38:41 -0700
Last modified on 2021-03-17 14:22:37 -0700
Version 11.0
Hardware PC Linux
CC htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
Fixed by commit(s)
Attachments test.cc (1493 bytes, text/x-c++src)
Blocks
Blocked by
See also
Created attachment 24646
example program

Exactly as the title states, assuming that getData(i) returns a shared_ptr<foo>
that is stored in an underlying vector, el gets destroyed, effectively leaving
the vector in a corrupted state.
This occurs only in the presence of the continue, without it (e.g. when
printing inside the condition), everything works fine. Same if el in not
created in the condition check.
Reporting against v.11, but when fiddling with godbolt the bug seems to have
been there since forever and is replicable for C++11 and any newer standard.

void testLoop()
{
    std::cout << __FUNCTION__ << " start" << '\n';
    Holder h;
    for (auto i = 0u; auto el = h.getData(i); ++i) {
        if (i %2)
            continue;
        h.getData(i)->print();
    }
    std::cout << __FUNCTION__ << " end" << '\n';
}

It has initially been found for MSVC, if it is related:
https://stackoverflow.com/questions/66382186/possible-msvc-compiler-bug#
https://godbolt.org/z/T43n4e
Quuxplusone commented 3 years ago

Attached test.cc (1493 bytes, text/x-c++src): example program

Quuxplusone commented 3 years ago

Somewhat reduced: https://godbolt.org/z/veEzx9

Wow. So, we're emitting code for 'continue;' that branches through the cleanup for the condition variable to the increment expression. But the increment expression also branches through that cleanup to the start of the loop. So we run the destructor twice.

Quuxplusone commented 3 years ago

https://reviews.llvm.org/D98816