<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 - Miscompilation of C++ constructors"
   href="https://bugs.llvm.org/show_bug.cgi?id=40118">40118</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Miscompilation of C++ constructors
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>7.0
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>tebbi@chromium.org
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=21259" name="attach_21259" title="minimized repro for the bug">attachment 21259</a> <a href="attachment.cgi?id=21259&action=edit" title="minimized repro for the bug">[details]</a></span>
minimized repro for the bug

The following (minimized) code is miscompiled in Clang 7.0, as well as the
trunk version currently used in Chromium (clang version 8.0.0 (trunk 346388),
Target: x86_64-pc-windows-msvc, Thread model: posix), when compiled with
optimizations, on Windows, and in 32 bit mode.


#include <memory>
#include <string>
#include <iostream>

struct Foo {
  Foo(std::string s) : x(std::move(s)){}
  std::string x;
};

struct Container {
  Container(Foo foo) : storage(new Foo(std::move(foo))) {}
  std::unique_ptr<Foo> storage;
};

int main() {
  Container c(Foo("hi there!"));
  std::cout << c.storage->x.size() << std::endl;
}


The compiled program crashes with memory corruption, the std::string accessed
in the last line points to random memory.

I can reproduce it by compiling as follows:
clang-cl.exe -D_HAS_ITERATOR_DEBUGGING=0 -m32  /MTd /O2 clang-bug.cc

At the moment, we work around the bug by writing the constructor as 
Foo(std::string s) { x = std::move(s); }
which strangely enough seems to make a difference.</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>