authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-05 12:47:59-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-05 12:48:29-05:00
log40e4e42a669a5312cf9400a5e96d6288b44be286
tree3f2a2a284dc7f8ca3a8da569a54fe4d674a09e6c
parent44d8d654a0ba463a1d4cf34d435c8422bfcd1c81

handle linux returning EINVAL for large reads

see #743

1 files changed, 8 insertions(+), 3 deletions(-)

std/os/index.zig+8-3
...@@ -189,10 +189,15 @@ pub fn close(handle: FileHandle) void {...@@ -189,10 +189,15 @@ pub fn close(handle: FileHandle) void {
189189
190/// Calls POSIX read, and keeps trying if it gets interrupted.190/// Calls POSIX read, and keeps trying if it gets interrupted.
191pub fn posixRead(fd: i32, buf: []u8) %void {191pub fn posixRead(fd: i32, buf: []u8) %void {
192 // Linux can return EINVAL when read amount is > 0x7ffff000
193 // See https://github.com/zig-lang/zig/pull/743#issuecomment-363158274
194 const max_buf_len = 0x7ffff000;
195
192 var index: usize = 0;196 var index: usize = 0;
193 while (index < buf.len) {197 while (index < buf.len) {
194 const amt_written = posix.read(fd, &buf[index], buf.len - index);198 const want_to_read = math.min(buf.len - index, usize(max_buf_len));
195 const err = posix.getErrno(amt_written);199 const rc = posix.read(fd, &buf[index], want_to_read);
200 const err = posix.getErrno(rc);
196 if (err > 0) {201 if (err > 0) {
197 return switch (err) {202 return switch (err) {
198 posix.EINTR => continue,203 posix.EINTR => continue,
...@@ -205,7 +210,7 @@ pub fn posixRead(fd: i32, buf: []u8) %void {...@@ -205,7 +210,7 @@ pub fn posixRead(fd: i32, buf: []u8) %void {
205 else => unexpectedErrorPosix(err),210 else => unexpectedErrorPosix(err),
206 };211 };
207 }212 }
208 index += amt_written;213 index += rc;
209 }214 }
210}215}
211216