<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 - _LIBCPP_DEBUG=1 std::transform in-place: abort() (Attempted to dereference a non-dereferenceable iterator)"
   href="https://bugs.llvm.org/show_bug.cgi?id=41274">41274</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>_LIBCPP_DEBUG=1 std::transform in-place: abort() (Attempted to dereference a non-dereferenceable iterator)
          </td>
        </tr>

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

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

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

        <tr>
          <th>OS</th>
          <td>MacOS X
          </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>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>andi.schulz@me.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>When using std::transform on a copy-initialized std::string to transform it
in-place, compiling the program with -D_LIBCPP_DEBUG=1 makes the iterator
checks at runtime fail. When compiling without the debug flag, the program
works both optimized and unoptimized as expected. I'm using the LLVM 8.0.0
release build on macOS, installed to /usr/local/opt/llvm.


Example program:

#include <algorithm>
#include <iostream>

int main(int argc, char *argv[]) {
    std::string source = "hello";

    std::string the_copy = source;

    std::transform(source.begin(), source.end(), source.begin(), [](unsigned
char c) -> unsigned char {
        return std::toupper(c);
    });

    std::cout << "transformed source: " << source << "\n\n";

    std::transform(the_copy.begin(), the_copy.end(), the_copy.begin(),
[](unsigned char c) -> unsigned char {
        return std::toupper(c);
    });

    std::cout << "transformed copy: " << the_copy << "\n\n";
}


Compile command:

/usr/local/opt/llvm/bin/clang++ main.cpp -o main -D_LIBCPP_DEBUG=1 -g -isysroot
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk
/usr/local/opt/llvm/lib/libc++.a -lc++abi -nodefaultlibs -lSystem -std=c++17


Output:

transformed source: HELLO

/usr/local/opt/llvm/bin/../include/c++/v1/iterator:1372: _LIBCPP_ASSERT
'__get_const_db()->__dereferenceable(this)' failed. Attempted to dereference a
non-dereferenceable iterator
[1]    32560 abort      ./main</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>