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 {
189189
190190/// Calls POSIX read, and keeps trying if it gets interrupted.
191191pub 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
192196 var index: usize = 0;
193197 while (index < buf.len) {
194 const amt_written = posix.read(fd, &buf[index], buf.len - index);
195 const err = posix.getErrno(amt_written);
198 const want_to_read = math.min(buf.len - index, usize(max_buf_len));
199 const rc = posix.read(fd, &buf[index], want_to_read);
200 const err = posix.getErrno(rc);
196201 if (err > 0) {
197202 return switch (err) {
198203 posix.EINTR => continue,
......@@ -205,7 +210,7 @@ pub fn posixRead(fd: i32, buf: []u8) %void {
205210 else => unexpectedErrorPosix(err),
206211 };
207212 }
208 index += amt_written;
213 index += rc;
209214 }
210215}
211216