<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Incorrect optimization of unique_ptr construction leads to segfault"
   href="https://bugs.llvm.org/show_bug.cgi?id=42561">42561</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Incorrect optimization of unique_ptr construction leads to segfault
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>8.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>jasonr@3db-labs.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>This comes from a Stack Overflow question I asked a long time ago:

<a href="https://stackoverflow.com/questions/43624400/is-this-unsafe-usage-of-a-braced-initializer-list-in-a-range-based-for-loop">https://stackoverflow.com/questions/43624400/is-this-unsafe-usage-of-a-braced-initializer-list-in-a-range-based-for-loop</a>

A minimal test case is as follows:

----------

#include <initializer_list>
#include <memory>

struct Test
{
    int x;
};

int main()
{
    std::unique_ptr<Test> a(new Test);
    std::unique_ptr<Test> b(new Test);
    std::unique_ptr<Test> c(new Test);
    int id = 0;
    for(auto t : std::initializer_list<Test*>({a.get(), b.get(), c.get()}))
        t->x = id++;
    return 0;
}

----------

I compiled the above code with "-O3 -std=c++11". What I'm seeing is that,
since, clang v3.9, the above program crashes with a segfault pn my x86_64
system. From looking at the assembly that is generated, it appears that the
construction of the std::unique_ptr objects is optimized out, but the for loop
that dereferences the unique_ptr objects is not elided. Thus, an uninitialized
pointer is dereferenced and a segfault ensues.

I have the example code up on Compiler Explorer to show how the compiler output
evolves between clang versions:

<a href="https://gcc.godbolt.org/z/_7MK9z">https://gcc.godbolt.org/z/_7MK9z</a>

It appears to work properly with versions before v3.9 (in which case it
optimizes out the entire program, which is reasonable).</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>