From 8078d8cd3fb989b0c7a26adde0ee2486e1f473f8 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 29 Dec 2020 14:02:12 -0700 Subject: [PATCH] std.ChildProcess: fix max_output_bytes handling The previous logic had a false positive of returning an error when in fact the maximum number of output bytes had not been exceeded. --- lib/std/child_process.zig | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/std/child_process.zig b/lib/std/child_process.zig index acab303f73..d911b8ca2b 100644 --- a/lib/std/child_process.zig +++ b/lib/std/child_process.zig @@ -212,16 +212,18 @@ pub const ChildProcess = struct { if (poll_fds[0].revents & os.POLLIN != 0) { // stdout is ready. const new_capacity = std.math.min(stdout.items.len + bump_amt, max_output_bytes); - if (new_capacity == stdout.capacity) return error.StdoutStreamTooLong; try stdout.ensureCapacity(new_capacity); - stdout.items.len += try os.read(poll_fds[0].fd, stdout.unusedCapacitySlice()); + const buf = stdout.unusedCapacitySlice(); + if (buf.len == 0) return error.StdoutStreamTooLong; + stdout.items.len += try os.read(poll_fds[0].fd, buf); } if (poll_fds[1].revents & os.POLLIN != 0) { // stderr is ready. const new_capacity = std.math.min(stderr.items.len + bump_amt, max_output_bytes); - if (new_capacity == stderr.capacity) return error.StderrStreamTooLong; try stderr.ensureCapacity(new_capacity); - stderr.items.len += try os.read(poll_fds[1].fd, stderr.unusedCapacitySlice()); + const buf = stderr.unusedCapacitySlice(); + if (buf.len == 0) return error.StderrStreamTooLong; + stderr.items.len += try os.read(poll_fds[1].fd, buf); } // Exclude the fds that signaled an error.