https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118162
Bug ID: 118162
Summary: ofstream::write(…, count) fails when count >= 2^31 on
darwin
Product: gcc
Version: 15.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: mimomorin at gmail dot com
Target Milestone: ---
Created attachment 59937
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=59937&action=edit
Testcase for ofstream::write(…, count >= 2^31)
This is a companion PR to PR102259.
On macOS, `ofstream::write(…, count >= 2^31)` fails without partial write.
Here is the attached test case:
#include <iostream>
#include <fstream>
int main() {
auto buffer = new char[1LL << 31];
std::ofstream os{"2GiB.bin", std::ios::binary};
os.write(buffer, 1LL << 31);
std::cout << os.good() << " (" << os.tellp() << " bytes)\n";
// Expected output: "1 (2147483648 bytes)"
// Actual output on macOS 11 or newer: "0 (-1 bytes)"
}
Here are the manpages for `write` and `writev` in macOS and FreeBSD:
[macOS manpage]
https://keith.github.io/xcode-man-pages/write.2.html
… write() … will fail and the file pointer will remain unchanged if:
The value provided for nbyte exceeds INT_MAX.
… writev() … may also return the following errors:
The sum of the iov_len values in the iov array overflows a 32-bit integer.
[FreeBSD manpage]
https://man.freebsd.org/cgi/man.cgi?query=writev
… write(), writev() … will fail and the file pointer will remain unchanged:
The value nbytes is greater than SSIZE_MAX
(or greater than INT_MAX, if the sysctl debug.iosize_max_clamp is non-zero).
… writev() … may return one of the following errors:
The sum of the iov_len values is greater than SSIZE_MAX
(or greater than INT_MAX, if the sysctl debug.iosize_max_clamp is non-zero).