<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 - Undefined behaviour in ifstream assignment operator"
   href="https://bugs.llvm.org/show_bug.cgi?id=50445">50445</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Undefined behaviour in ifstream assignment operator
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>12.0
          </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>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>tonyelewis@hotmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Compiling the following code with `clang++ -g -std=c++17 -stdlib=libc++
-fsanitize=undefined a.cpp -o a.clang_bin && ./a.clang_bin` :


#include <filesystem>
#include <fstream>

int main() {
        ::std::ifstream the_ifstream;
        the_ifstream = ::std::ifstream( ::std::filesystem::path{ "txt1" } );
        the_ifstream = ::std::ifstream( ::std::filesystem::path{ "txt2" } );
}


…and then running it (after creating local files txt1 and txt2 and settting
environment variable UBSAN_OPTIONS to `print_stacktrace=1`), I get:


/llvm/bin/../include/c++/v1/fstream:445:35: runtime error: applying non-zero
offset to non-null pointer 0x0000022de3c0 produced null pointer
    #0 0x446357 in std::__1::basic_filebuf<char, std::__1::char_traits<char>
<span class="quote">>::swap(std::__1::basic_filebuf<char, std::__1::char_traits<char> >&)</span >
/llvm/bin/../include/c++/v1/fstream:445:35
    #1 0x4444d3 in std::__1::basic_filebuf<char, std::__1::char_traits<char>
<span class="quote">>::operator=(std::__1::basic_filebuf<char, std::__1::char_traits<char> >&&)</span >
/llvm/bin/../include/c++/v1/fstream:392:5
    #2 0x42e23d in std::__1::basic_ifstream<char, std::__1::char_traits<char>
<span class="quote">>::operator=(std::__1::basic_ifstream<char, std::__1::char_traits<char> >&&)</span >
/llvm/bin/../include/c++/v1/fstream:1259:11
    #3 0x42d9e4 in main /tmp/a.cpp:7:15
    #4 0x7f9677cf20b2 in __libc_start_main
/build/glibc-YbNSs7/glibc-2.31/csu/../csu/libc-start.c:308:16
    #5 0x40564d in _start (/tmp/a.clang_bin+0x40564d)

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior
/llvm/bin/../include/c++/v1/fstream:445:35 in 
/llvm/bin/../include/c++/v1/fstream:446:34: runtime error: applying non-zero
offset to non-null pointer 0x0000022de3c0 produced null pointer
    #0 0x446501 in std::__1::basic_filebuf<char, std::__1::char_traits<char>
<span class="quote">>::swap(std::__1::basic_filebuf<char, std::__1::char_traits<char> >&)</span >
/llvm/bin/../include/c++/v1/fstream:446:34
    #1 0x4444d3 in std::__1::basic_filebuf<char, std::__1::char_traits<char>
<span class="quote">>::operator=(std::__1::basic_filebuf<char, std::__1::char_traits<char> >&&)</span >
/llvm/bin/../include/c++/v1/fstream:392:5
    #2 0x42e23d in std::__1::basic_ifstream<char, std::__1::char_traits<char>
<span class="quote">>::operator=(std::__1::basic_ifstream<char, std::__1::char_traits<char> >&&)</span >
/llvm/bin/../include/c++/v1/fstream:1259:11
    #3 0x42d9e4 in main /tmp/a.cpp:7:15
    #4 0x7f9677cf20b2 in __libc_start_main
/build/glibc-YbNSs7/glibc-2.31/csu/../csu/libc-start.c:308:16
    #5 0x40564d in _start (/tmp/a.clang_bin+0x40564d)

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior
/llvm/bin/../include/c++/v1/fstream:446:34 in 
/llvm/bin/../include/c++/v1/fstream:447:47: runtime error: pointer index
expression with base 0x7ffcc28d84f8 overflowed to 0xfffffffffffffd38
    #0 0x446656 in std::__1::basic_filebuf<char, std::__1::char_traits<char>
<span class="quote">>::swap(std::__1::basic_filebuf<char, std::__1::char_traits<char> >&)</span >
/llvm/bin/../include/c++/v1/fstream:447:47
    #1 0x4444d3 in std::__1::basic_filebuf<char, std::__1::char_traits<char>
<span class="quote">>::operator=(std::__1::basic_filebuf<char, std::__1::char_traits<char> >&&)</span >
/llvm/bin/../include/c++/v1/fstream:392:5
    #2 0x42e23d in std::__1::basic_ifstream<char, std::__1::char_traits<char>
<span class="quote">>::operator=(std::__1::basic_ifstream<char, std::__1::char_traits<char> >&&)</span >
/llvm/bin/../include/c++/v1/fstream:1259:11
    #3 0x42d9e4 in main /tmp/a.cpp:7:15
    #4 0x7f9677cf20b2 in __libc_start_main
/build/glibc-YbNSs7/glibc-2.31/csu/../csu/libc-start.c:308:16
    #5 0x40564d in _start (/tmp/a.clang_bin+0x40564d)

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior
/llvm/bin/../include/c++/v1/fstream:447:47 in 
/llvm/bin/../include/c++/v1/fstream:448:46: runtime error: pointer index
expression with base 0x7ffcc28d84f8 overflowed to 0xfffffffffffffd38
    #0 0x446752 in std::__1::basic_filebuf<char, std::__1::char_traits<char>
<span class="quote">>::swap(std::__1::basic_filebuf<char, std::__1::char_traits<char> >&)</span >
/llvm/bin/../include/c++/v1/fstream:448:46
    #1 0x4444d3 in std::__1::basic_filebuf<char, std::__1::char_traits<char>
<span class="quote">>::operator=(std::__1::basic_filebuf<char, std::__1::char_traits<char> >&&)</span >
/llvm/bin/../include/c++/v1/fstream:392:5
    #2 0x42e23d in std::__1::basic_ifstream<char, std::__1::char_traits<char>
<span class="quote">>::operator=(std::__1::basic_ifstream<char, std::__1::char_traits<char> >&&)</span >
/llvm/bin/../include/c++/v1/fstream:1259:11
    #3 0x42d9e4 in main /tmp/a.cpp:7:15
    #4 0x7f9677cf20b2 in __libc_start_main
/build/glibc-YbNSs7/glibc-2.31/csu/../csu/libc-start.c:308:16
    #5 0x40564d in _start (/tmp/a.clang_bin+0x40564d)

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior
/llvm/bin/../include/c++/v1/fstream:448:46 in


I'm using a build of a recent commit (34c098b780a27a90b5614ea3b949b9269835f2a5,
11th May, 2021).

The issue can also be seen on trunk on Compiler Explorer :
<a href="https://godbolt.org/z/h5zc8jsvd">https://godbolt.org/z/h5zc8jsvd</a>


Thank you very much for all work on libc++.</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>