<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 --- - std::string::resize with (max_size - 1) segmentation fault"
   href="http://llvm.org/bugs/show_bug.cgi?id=17148">17148</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>std::string::resize with (max_size - 1) segmentation fault
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>unspecified
          </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>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>ruslan_baratov@yahoo.com
          </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>This simple program produce segmentation fault:

#include <string>
#include <stdexcept>

int main() {
  try {
    std::string s;
    s.resize(s.max_size() - 1, 'x');
    return 0;
  }
  catch (std::bad_alloc& exc) {
    return 1;
  }
}

Expected return: 0 or 1 (bad_alloc).

valgrind output:

==35098== Invalid write of size 4
==35098==    at 0x4C2F9BF: memset (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==35098==    by 0x4016AD: std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> >::append(unsigned long,
char) (string:666)
==35098==    by 0x40115A: std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> >::resize(unsigned long,
char) (string:3038)
==35098==    by 0x400F68: main (test.cpp:7)

Changing '-libstd=libc++' to '-stdlib=libstdc++' help, return is 1 (bad_alloc).

build from git:
clang: b2c70470def1ab0380be36a295ce4aa7864abfe1
llvm: a83aeae350d822ec84ded34ff3fdf1e0917d3bfd
libcxx: 62f34be0baf276c2b310db8bda0d358841ebab9a</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>