<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 - Swapping a closed filebuf runs into undefined behavior"
   href="https://bugs.llvm.org/show_bug.cgi?id=49923">49923</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Swapping a closed filebuf runs into undefined behavior
          </td>
        </tr>

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

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

        <tr>
          <th>Reporter</th>
          <td>alex@grundis.de
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>In the following Code UBsan is triggered:

        std::ofstream f_old(filename);
        assert(f_old << "Hello ");

        std::ofstream f_new(filename2);
        assert(f_new << "Foo ");

        // After swapping both are valid and where they left
        f_new.swap(f_old);
        assert(f_old << "Bar");
        assert(f_new << "World");

        f_new.close();
        swap(f_new, f_old);

The problem is as follows:
- __extbuf_ is set to some buffer
- during operation of the filebug __extbufnext_ and __extbufend_ are set
relative to that
- filebuf.close calls setbuf(0, 0) which ends up setting __extbuf_ to the
internal min buffer:
<a href="https://github.com/llvm/llvm-project/blob/82e537a9d28a2c18bd1637e2eac0e0af658ed829/libcxx/include/fstream#L896">https://github.com/llvm/llvm-project/blob/82e537a9d28a2c18bd1637e2eac0e0af658ed829/libcxx/include/fstream#L896</a>
- Note that it does not reset the next/end members
- filebuf.swap then uses the difference between __extbufnext_/__extbufend_  to
__extbuf_  which is invalid as they don't point to the same memory anymore:
<a href="https://github.com/llvm/llvm-project/blob/release/12.x/libcxx/include/fstream#L428-L429">https://github.com/llvm/llvm-project/blob/release/12.x/libcxx/include/fstream#L428-L429</a>
- This then triggers UBsan in the following pointer addition with that invalid
offset:
<a href="https://github.com/llvm/llvm-project/blob/release/12.x/libcxx/include/fstream#L442-L445">https://github.com/llvm/llvm-project/blob/release/12.x/libcxx/include/fstream#L442-L445</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>