<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </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 --- - Sigsegv in exception::what() after r"
   href="http://llvm.org/bugs/show_bug.cgi?id=18403">18403</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Sigsegv in exception::what() after r
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libc++
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </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>All Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>hhinnant@apple.com
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>jonathan.sauer@gmx.de
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The following program, when compiled with clang r198596 and libc++ r198635
(which was also compiled with clang r198596), crashes with a SIGSEGV when run:

#include <cstdio>
#include <stdexcept>

int main()
{
    try
    {
        throw std::runtime_error("Error");
    }
    catch (const std::exception& e)
    {
        std::printf("%s\n", e.what());
    }
}


This results in:

% ~/LLVM/build/Release+Asserts/bin/clang++ -std=c++11 -stdlib=libc++ clang.cpp
% gdb ./a.out
[...]
(gdb) r
[...]
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000
0x00000001000425e6 in std::runtime_error::what ()
(gdb) bt
#0  0x00000001000425e6 in std::runtime_error::what ()
#1  0x0000000100000da3 in main ()


After reverting to r198504 (before the last change to stdexcept.cpp), the code
ran fine.

I suspect the cause of the crash lies in the following change in
stdexcept.cpp[*]:

 runtime_error::runtime_error(const runtime_error& le) _NOEXCEPT
 {
-    __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
-    ::new(&s) __libcpp_nmstr((const __libcpp_nmstr&)le.__imp_);
+    __libcpp_nmstr *s = static_cast<__libcpp_nmstr *>(__imp_);
+    const __libcpp_nmstr *s2 = static_cast<const __libcpp_nmstr *>(le.__imp_);
+    ::new(s) __libcpp_nmstr(*s2);
 }

Before the new "__libcpp_nmstr" was created inside "__imp_" (as "s" was a
reference to it, and taking the address of a reference means taking the address
of the referenced variable, i.e. "__imp_"). Now the new string is created
inside "s" and never gets stored in "__imp_".

Proposed fix: Remove "s" and just say:

  ::new(__imp_) __libcpp_nmstr(*s2);

(same in the other constructors as well)

[*]
<<a href="http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/stdexcept.cpp?r1=198505&r2=198504&pathrev=198505">http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/stdexcept.cpp?r1=198505&r2=198504&pathrev=198505</a>></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>