[llvm-bugs] [Bug 49585] New: shared_ptr is released when used in for loop's condition statement and continue is used in loop's body

via llvm-bugs llvm-bugs at lists.llvm.org
Sun Mar 14 06:38:41 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=49585

            Bug ID: 49585
           Summary: shared_ptr is released when used in for loop's
                    condition statement and continue is used in loop's
                    body
           Product: clang
           Version: 11.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: ammiera at hotmail.com
                CC: htmldeveloper at gmail.com, llvm-bugs at lists.llvm.org,
                    neeilans at live.com, richard-llvm at metafoo.co.uk

Created attachment 24646
  --> https://bugs.llvm.org/attachment.cgi?id=24646&action=edit
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

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210314/0a42cc49/attachment.html>


More information about the llvm-bugs mailing list