authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-09-13 09:42:00+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-13 16:31:21-04:00
log5e50d145d964238a68d4780e253d26431e7c7994
tree4f7cbc281a0efeb742372c36f8c2dd9f41a5f988
parent9fe4c89230df2d78c8bf37b4b1d7a9bedb92677b

std: Limit the read/write size on Darwin

It turns out that the kernel won't read or write more than 0x7fffffff bytes in a single call, failing with EINVAL when trying to do so. Adjust the limit and curse whoever is responsible for this. Closes #6332

1 files changed, 9 insertions(+), 1 deletions(-)

lib/std/os.zig+9-1
......@@ -320,6 +320,7 @@ pub const ReadError = error{
320320/// Linux has a limit on how many bytes may be transferred in one `read` call, which is `0x7ffff000`
321321/// on both 64-bit and 32-bit systems. This is due to using a signed C int as the return value, as
322322/// well as stuffing the errno codes into the last `4096` values. This is noted on the `read` man page.
323/// The limit on Darwin is `0x7fffffff`, trying to read more than that returns EINVAL.
323324/// For POSIX the limit is `math.maxInt(isize)`.
324325pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
325326 if (builtin.os.tag == .windows) {
......@@ -353,6 +354,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
353354 // Prevents EINVAL.
354355 const max_count = switch (std.Target.current.os.tag) {
355356 .linux => 0x7ffff000,
357 .macosx, .ios, .watchos, .tvos => math.maxInt(i32),
356358 else => math.maxInt(isize),
357359 };
358360 const adjusted_len = math.min(max_count, buf.len);
......@@ -693,6 +695,7 @@ pub const WriteError = error{
693695/// Linux has a limit on how many bytes may be transferred in one `write` call, which is `0x7ffff000`
694696/// on both 64-bit and 32-bit systems. This is due to using a signed C int as the return value, as
695697/// well as stuffing the errno codes into the last `4096` values. This is noted on the `write` man page.
698/// The limit on Darwin is `0x7fffffff`, trying to read more than that returns EINVAL.
696699/// The corresponding POSIX limit is `math.maxInt(isize)`.
697700pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
698701 if (builtin.os.tag == .windows) {
......@@ -726,6 +729,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
726729
727730 const max_count = switch (std.Target.current.os.tag) {
728731 .linux => 0x7ffff000,
732 .macosx, .ios, .watchos, .tvos => math.maxInt(i32),
729733 else => math.maxInt(isize),
730734 };
731735 const adjusted_len = math.min(max_count, bytes.len);
......@@ -851,6 +855,7 @@ pub const PWriteError = WriteError || error{Unseekable};
851855/// Linux has a limit on how many bytes may be transferred in one `pwrite` call, which is `0x7ffff000`
852856/// on both 64-bit and 32-bit systems. This is due to using a signed C int as the return value, as
853857/// well as stuffing the errno codes into the last `4096` values. This is noted on the `write` man page.
858/// The limit on Darwin is `0x7fffffff`, trying to write more than that returns EINVAL.
854859/// The corresponding POSIX limit is `math.maxInt(isize)`.
855860pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {
856861 if (std.Target.current.os.tag == .windows) {
......@@ -888,6 +893,7 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {
888893 // Prevent EINVAL.
889894 const max_count = switch (std.Target.current.os.tag) {
890895 .linux => 0x7ffff000,
896 .macosx, .ios, .watchos, .tvos => math.maxInt(i32),
891897 else => math.maxInt(isize),
892898 };
893899 const adjusted_len = math.min(max_count, bytes.len);
......@@ -3084,7 +3090,7 @@ pub fn connect(sockfd: socket_t, sock_addr: *const sockaddr, len: socklen_t) Con
30843090 .WSAECONNREFUSED => return error.ConnectionRefused,
30853091 .WSAETIMEDOUT => return error.ConnectionTimedOut,
30863092 .WSAEHOSTUNREACH // TODO: should we return NetworkUnreachable in this case as well?
3087 , .WSAENETUNREACH => return error.NetworkUnreachable,
3093 , .WSAENETUNREACH => return error.NetworkUnreachable,
30883094 .WSAEFAULT => unreachable,
30893095 .WSAEINVAL => unreachable,
30903096 .WSAEISCONN => unreachable,
......@@ -4711,6 +4717,7 @@ fn count_iovec_bytes(iovs: []const iovec_const) usize {
47114717/// Linux has a limit on how many bytes may be transferred in one `sendfile` call, which is `0x7ffff000`
47124718/// on both 64-bit and 32-bit systems. This is due to using a signed C int as the return value, as
47134719/// well as stuffing the errno codes into the last `4096` values. This is cited on the `sendfile` man page.
4720/// The limit on Darwin is `0x7fffffff`, trying to write more than that returns EINVAL.
47144721/// The corresponding POSIX limit on this is `math.maxInt(isize)`.
47154722pub fn sendfile(
47164723 out_fd: fd_t,
......@@ -4733,6 +4740,7 @@ pub fn sendfile(
47334740 });
47344741 const max_count = switch (std.Target.current.os.tag) {
47354742 .linux => 0x7ffff000,
4743 .macosx, .ios, .watchos, .tvos => math.maxInt(i32),
47364744 else => math.maxInt(size_t),
47374745 };
47384746