authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-02-07 16:11:57+11:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-06 18:49:12-05:00
log8f627593ebb7ce073e43f06235fe1d9b9c1a1579
treeacc6c57ae7720c7109f079ebe4f47d5dc57300cf
parentbcf56c32eb11306613c92128c1b95ff280ba4f68
signaturelock-open Commit is signed but in an unrecognized format.

Use in_stream.readAllAlloc where sensible


4 files changed, 37 insertions(+), 38 deletions(-)

lib/std/build.zig+3-5
...@@ -926,11 +926,9 @@ pub const Builder = struct {...@@ -926,11 +926,9 @@ pub const Builder = struct {
926926
927 try child.spawn();927 try child.spawn();
928928
929 var stdout = std.Buffer.initNull(self.allocator);
930 defer std.Buffer.deinit(&stdout);
931
932 var stdout_file_in_stream = child.stdout.?.inStream();929 var stdout_file_in_stream = child.stdout.?.inStream();
933 try stdout_file_in_stream.stream.readAllBuffer(&stdout, max_output_size);930 const stdout = try stdout_file_in_stream.stream.readAllAlloc(self.allocator, max_output_size);
931 errdefer self.allocator.free(stdout);
934932
935 const term = try child.wait();933 const term = try child.wait();
936 switch (term) {934 switch (term) {
...@@ -939,7 +937,7 @@ pub const Builder = struct {...@@ -939,7 +937,7 @@ pub const Builder = struct {
939 out_code.* = @truncate(u8, code);937 out_code.* = @truncate(u8, code);
940 return error.ExitCodeFailure;938 return error.ExitCodeFailure;
941 }939 }
942 return stdout.toOwnedSlice();940 return stdout;
943 },941 },
944 .Signal, .Stopped, .Unknown => |code| {942 .Signal, .Stopped, .Unknown => |code| {
945 out_code.* = @truncate(u8, code);943 out_code.* = @truncate(u8, code);
lib/std/build/run.zig+21-14
...@@ -169,26 +169,33 @@ pub const RunStep = struct {...@@ -169,26 +169,33 @@ pub const RunStep = struct {
169 return err;169 return err;
170 };170 };
171171
172 var stdout = Buffer.initNull(self.builder.allocator);
173 var stderr = Buffer.initNull(self.builder.allocator);
174
175 // TODO need to poll to read these streams to prevent a deadlock (or rely on evented I/O).172 // TODO need to poll to read these streams to prevent a deadlock (or rely on evented I/O).
176173
174 var stdout: []const u8 = undefined;
177 switch (self.stdout_action) {175 switch (self.stdout_action) {
178 .expect_exact, .expect_matches => {176 .expect_exact, .expect_matches => {
179 var stdout_file_in_stream = child.stdout.?.inStream();177 var stdout_file_in_stream = child.stdout.?.inStream();
180 stdout_file_in_stream.stream.readAllBuffer(&stdout, max_stdout_size) catch unreachable;178 stdout = stdout_file_in_stream.stream.readAllAlloc(self.builder.allocator, max_stdout_size) catch unreachable;
181 },179 },
182 .inherit, .ignore => {},180 .inherit, .ignore => {},
183 }181 }
182 defer switch (self.stdout_action) {
183 .expect_exact, .expect_matches => self.builder.allocator.free(stdout),
184 .inherit, .ignore => {},
185 };
184186
185 switch (self.stdout_action) {187 var stderr: []const u8 = undefined;
188 switch (self.stdout_behavior) {
186 .expect_exact, .expect_matches => {189 .expect_exact, .expect_matches => {
187 var stderr_file_in_stream = child.stderr.?.inStream();190 var stderr_file_in_stream = child.stderr.?.inStream();
188 stderr_file_in_stream.stream.readAllBuffer(&stderr, max_stdout_size) catch unreachable;191 stderr = stderr_file_in_stream.stream.readAllAlloc(self.builder.allocator, max_stdout_size) catch unreachable;
189 },192 },
190 .inherit, .ignore => {},193 .inherit, .ignore => {},
191 }194 }
195 defer switch (self.stderr_action) {
196 .expect_exact, .expect_matches => self.builder.allocator.free(stderr),
197 .inherit, .ignore => {},
198 };
192199
193 const term = child.wait() catch |err| {200 const term = child.wait() catch |err| {
194 warn("Unable to spawn {}: {}\n", .{ argv[0], @errorName(err) });201 warn("Unable to spawn {}: {}\n", .{ argv[0], @errorName(err) });
...@@ -216,7 +223,7 @@ pub const RunStep = struct {...@@ -216,7 +223,7 @@ pub const RunStep = struct {
216 switch (self.stderr_action) {223 switch (self.stderr_action) {
217 .inherit, .ignore => {},224 .inherit, .ignore => {},
218 .expect_exact => |expected_bytes| {225 .expect_exact => |expected_bytes| {
219 if (!mem.eql(u8, expected_bytes, stderr.toSliceConst())) {226 if (!mem.eql(u8, expected_bytes, stderr)) {
220 warn(227 warn(
221 \\228 \\
222 \\========= Expected this stderr: =========229 \\========= Expected this stderr: =========
...@@ -224,13 +231,13 @@ pub const RunStep = struct {...@@ -224,13 +231,13 @@ pub const RunStep = struct {
224 \\========= But found: ====================231 \\========= But found: ====================
225 \\{}232 \\{}
226 \\233 \\
227 , .{ expected_bytes, stderr.toSliceConst() });234 , .{ expected_bytes, stderr });
228 printCmd(cwd, argv);235 printCmd(cwd, argv);
229 return error.TestFailed;236 return error.TestFailed;
230 }237 }
231 },238 },
232 .expect_matches => |matches| for (matches) |match| {239 .expect_matches => |matches| for (matches) |match| {
233 if (mem.indexOf(u8, stderr.toSliceConst(), match) == null) {240 if (mem.indexOf(u8, stderr, match) == null) {
234 warn(241 warn(
235 \\242 \\
236 \\========= Expected to find in stderr: =========243 \\========= Expected to find in stderr: =========
...@@ -238,7 +245,7 @@ pub const RunStep = struct {...@@ -238,7 +245,7 @@ pub const RunStep = struct {
238 \\========= But stderr does not contain it: =====245 \\========= But stderr does not contain it: =====
239 \\{}246 \\{}
240 \\247 \\
241 , .{ match, stderr.toSliceConst() });248 , .{ match, stderr });
242 printCmd(cwd, argv);249 printCmd(cwd, argv);
243 return error.TestFailed;250 return error.TestFailed;
244 }251 }
...@@ -248,7 +255,7 @@ pub const RunStep = struct {...@@ -248,7 +255,7 @@ pub const RunStep = struct {
248 switch (self.stdout_action) {255 switch (self.stdout_action) {
249 .inherit, .ignore => {},256 .inherit, .ignore => {},
250 .expect_exact => |expected_bytes| {257 .expect_exact => |expected_bytes| {
251 if (!mem.eql(u8, expected_bytes, stdout.toSliceConst())) {258 if (!mem.eql(u8, expected_bytes, stdout)) {
252 warn(259 warn(
253 \\260 \\
254 \\========= Expected this stdout: =========261 \\========= Expected this stdout: =========
...@@ -256,13 +263,13 @@ pub const RunStep = struct {...@@ -256,13 +263,13 @@ pub const RunStep = struct {
256 \\========= But found: ====================263 \\========= But found: ====================
257 \\{}264 \\{}
258 \\265 \\
259 , .{ expected_bytes, stdout.toSliceConst() });266 , .{ expected_bytes, stdout });
260 printCmd(cwd, argv);267 printCmd(cwd, argv);
261 return error.TestFailed;268 return error.TestFailed;
262 }269 }
263 },270 },
264 .expect_matches => |matches| for (matches) |match| {271 .expect_matches => |matches| for (matches) |match| {
265 if (mem.indexOf(u8, stdout.toSliceConst(), match) == null) {272 if (mem.indexOf(u8, stdout, match) == null) {
266 warn(273 warn(
267 \\274 \\
268 \\========= Expected to find in stdout: =========275 \\========= Expected to find in stdout: =========
...@@ -270,7 +277,7 @@ pub const RunStep = struct {...@@ -270,7 +277,7 @@ pub const RunStep = struct {
270 \\========= But stdout does not contain it: =====277 \\========= But stdout does not contain it: =====
271 \\{}278 \\{}
272 \\279 \\
273 , .{ match, stdout.toSliceConst() });280 , .{ match, stdout });
274 printCmd(cwd, argv);281 printCmd(cwd, argv);
275 return error.TestFailed;282 return error.TestFailed;
276 }283 }
lib/std/child_process.zig+7-9
...@@ -217,21 +217,19 @@ pub const ChildProcess = struct {...@@ -217,21 +217,19 @@ pub const ChildProcess = struct {
217217
218 try child.spawn();218 try child.spawn();
219219
220 var stdout = Buffer.initNull(args.allocator);
221 var stderr = Buffer.initNull(args.allocator);
222 defer Buffer.deinit(&stdout);
223 defer Buffer.deinit(&stderr);
224
225 var stdout_file_in_stream = child.stdout.?.inStream();220 var stdout_file_in_stream = child.stdout.?.inStream();
226 var stderr_file_in_stream = child.stderr.?.inStream();221 var stderr_file_in_stream = child.stderr.?.inStream();
227222
228 try stdout_file_in_stream.stream.readAllBuffer(&stdout, args.max_output_bytes);223 // TODO need to poll to read these streams to prevent a deadlock (or rely on evented I/O).
229 try stderr_file_in_stream.stream.readAllBuffer(&stderr, args.max_output_bytes);224 const stdout = try stdout_file_in_stream.stream.readAllAlloc(args.allocator, args.max_output_bytes);
225 errdefer args.allocator.free(stdout);
226 const stderr = try stderr_file_in_stream.stream.readAllAlloc(args.allocator, args.max_output_bytes);
227 errdefer args.allocator.free(stderr);
230228
231 return ExecResult{229 return ExecResult{
232 .term = try child.wait(),230 .term = try child.wait(),
233 .stdout = stdout.toOwnedSlice(),231 .stdout = stdout,
234 .stderr = stderr.toOwnedSlice(),232 .stderr = stderr,
235 };233 };
236 }234 }
237235
test/tests.zig+6-10
...@@ -566,14 +566,13 @@ pub const StackTracesContext = struct {...@@ -566,14 +566,13 @@ pub const StackTracesContext = struct {
566 }566 }
567 child.spawn() catch |err| debug.panic("Unable to spawn {}: {}\n", .{ full_exe_path, @errorName(err) });567 child.spawn() catch |err| debug.panic("Unable to spawn {}: {}\n", .{ full_exe_path, @errorName(err) });
568568
569 var stdout = Buffer.initNull(b.allocator);
570 var stderr = Buffer.initNull(b.allocator);
571
572 var stdout_file_in_stream = child.stdout.?.inStream();569 var stdout_file_in_stream = child.stdout.?.inStream();
573 var stderr_file_in_stream = child.stderr.?.inStream();570 var stderr_file_in_stream = child.stderr.?.inStream();
574571
575 stdout_file_in_stream.stream.readAllBuffer(&stdout, max_stdout_size) catch unreachable;572 const stdout = stdout_file_in_stream.stream.readAllAlloc(b.allocator, max_stdout_size) catch unreachable;
576 stderr_file_in_stream.stream.readAllBuffer(&stderr, max_stdout_size) catch unreachable;573 defer b.allocator.free(stdout);
574 const stderr = stderr_file_in_stream.stream.readAllAlloc(b.allocator, max_stdout_size) catch unreachable;
575 defer b.allocator.free(stderr);
577576
578 const term = child.wait() catch |err| {577 const term = child.wait() catch |err| {
579 debug.panic("Unable to spawn {}: {}\n", .{ full_exe_path, @errorName(err) });578 debug.panic("Unable to spawn {}: {}\n", .{ full_exe_path, @errorName(err) });
...@@ -616,11 +615,8 @@ pub const StackTracesContext = struct {...@@ -616,11 +615,8 @@ pub const StackTracesContext = struct {
616 const got: []const u8 = got_result: {615 const got: []const u8 = got_result: {
617 var buf = try Buffer.initSize(b.allocator, 0);616 var buf = try Buffer.initSize(b.allocator, 0);
618 defer buf.deinit();617 defer buf.deinit();
619 const bytes = if (stderr.endsWith("\n"))618 if (stderr.len != 0 and stderr[stderr.len - 1] == '\n') stderr = stderr[0 .. stderr.len - 1];
620 stderr.toSliceConst()[0 .. stderr.len() - 1]619 var it = mem.separate(stderr, "\n");
621 else
622 stderr.toSliceConst()[0..stderr.len()];
623 var it = mem.separate(bytes, "\n");
624 process_lines: while (it.next()) |line| {620 process_lines: while (it.next()) |line| {
625 if (line.len == 0) continue;621 if (line.len == 0) continue;
626 const delims = [_][]const u8{ ":", ":", ":", " in " };622 const delims = [_][]const u8{ ":", ":", ":", " in " };