<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 - std::basic_ostream::seekp() > 2GiB"
   href="https://bugs.llvm.org/show_bug.cgi?id=51867">51867</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>std::basic_ostream::seekp() > 2GiB
          </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>Windows NT
          </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>davidwe@posteo.de
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>When I use the CLANG64 environment, the "seekp()" function does not work
correctly.

Everything > 2147483648 bytes ( 2 GiB ) ends in an "ios_base::clear:
unspecified iostream_category error" exception.

When I compile the example with "-D_FILE_OFFSET_BITS=64", the seekp() function
works as expected.

As an example:

#include <filesystem>
#include <fstream>
#include <iostream>
#include <stdio.h>

#define MAX_FILE_SIZE_3GiB 3221225472
#define MAX_FILE_SIZE_2GiB 2147483648

int main(int argc, char** argv)
{
    try {
        std::filesystem::path sourceFile =
std::filesystem::temp_directory_path() / "example_3GiB.bin";

        std::ofstream ofs(sourceFile);
        ofs << "this is some text in the new file\n";
        ofs.close();

        std::filesystem::resize_file(sourceFile, MAX_FILE_SIZE_3GiB);

        std::fstream file(sourceFile);
        file.exceptions(std::ifstream::failbit | std::ifstream::badbit);

        std::cout << "File size:  " << std::filesystem::file_size(sourceFile)
<< '\n';

        // One byte less works.
        file.seekp(MAX_FILE_SIZE_2GiB - 1, std::ios_base::beg);
        std::cout << "File tellp(): " << file.tellp() << '\n';

        // Everything larger than 2 GiB ends in an exception
        file.seekp(MAX_FILE_SIZE_2GiB + 1, std::ios_base::beg);
        std::cout << "File tellp(): " << file.tellp() << '\n';

        file.close();
        std::filesystem::remove(sourceFile);
        return 0;
    } catch(std::fstream::failure const& ex) {
        std::cout << ex.what() << '\n';
        return 0;
    } catch(std::filesystem::filesystem_error const& ex) {
        std::cout << ex.what() << '\n';
        return 0;
    }
}


Output:

    File size: 3221225472
    File tellp(): 2147483647
    ios_base::clear: unspecified iostream_category error

clang -v:

    clang version 12.0.1
    Target: x86_64-w64-windows-gnu
    Thread model: posix
    InstalledDir: C:/msys64/clang64/bin


A discussion is already going on in github. (
<a href="https://github.com/msys2/MINGW-packages/issues/9585">https://github.com/msys2/MINGW-packages/issues/9585</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>