[llvm-bugs] [Bug 40118] New: Miscompilation of C++ constructors

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Dec 20 08:29:34 PST 2018


            Bug ID: 40118
           Summary: Miscompilation of C++ constructors
           Product: clang
           Version: 7.0
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: tebbi at chromium.org
                CC: htmldeveloper at gmail.com, llvm-bugs at lists.llvm.org,
                    neeilans at live.com, richard-llvm at metafoo.co.uk

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

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/20181220/5abe6dc6/attachment.html>

More information about the llvm-bugs mailing list