authorgravatar for 39384757+chwayne@users.noreply.github.comchwayne <39384757+chwayne@users.noreply.github.com> 2020-10-22 17:52:48-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-10-22 17:52:48-04:00
log1e13e8e8172c1409c5152e8b4e935c5a1d31704b
treeca4a50d7e30964118f0ddd6634ef13ec6933367e
parent2ee79f149b3a16aca152f6aa0c5a7a8df35c06de
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Make argsAlloc/ArgIterator return zero-sentinel strings (#6720)


1 files changed, 22 insertions(+), 21 deletions(-)

lib/std/process.zig+22-21
......@@ -194,7 +194,7 @@ pub const ArgIteratorPosix = struct {
194194 };
195195 }
196196
197 pub fn next(self: *ArgIteratorPosix) ?[]const u8 {
197 pub fn next(self: *ArgIteratorPosix) ?[:0]const u8 {
198198 if (self.index == self.count) return null;
199199
200200 const s = os.argv[self.index];
......@@ -213,7 +213,7 @@ pub const ArgIteratorPosix = struct {
213213pub const ArgIteratorWasi = struct {
214214 allocator: *mem.Allocator,
215215 index: usize,
216 args: [][]u8,
216 args: [][:0]u8,
217217
218218 pub const InitError = error{OutOfMemory} || os.UnexpectedError;
219219
......@@ -228,7 +228,7 @@ pub const ArgIteratorWasi = struct {
228228 };
229229 }
230230
231 fn internalInit(allocator: *mem.Allocator) InitError![][]u8 {
231 fn internalInit(allocator: *mem.Allocator) InitError![][:0]u8 {
232232 const w = os.wasi;
233233 var count: usize = undefined;
234234 var buf_size: usize = undefined;
......@@ -248,7 +248,7 @@ pub const ArgIteratorWasi = struct {
248248 else => |err| return os.unexpectedErrno(err),
249249 }
250250
251 var result_args = try allocator.alloc([]u8, count);
251 var result_args = try allocator.alloc([:0]u8, count);
252252 var i: usize = 0;
253253 while (i < count) : (i += 1) {
254254 result_args[i] = mem.spanZ(argv[i]);
......@@ -257,7 +257,7 @@ pub const ArgIteratorWasi = struct {
257257 return result_args;
258258 }
259259
260 pub fn next(self: *ArgIteratorWasi) ?[]const u8 {
260 pub fn next(self: *ArgIteratorWasi) ?[:0]const u8 {
261261 if (self.index == self.args.len) return null;
262262
263263 const arg = self.args[self.index];
......@@ -301,7 +301,7 @@ pub const ArgIteratorWindows = struct {
301301 }
302302
303303 /// You must free the returned memory when done.
304 pub fn next(self: *ArgIteratorWindows, allocator: *Allocator) ?(NextError![]u8) {
304 pub fn next(self: *ArgIteratorWindows, allocator: *Allocator) ?(NextError![:0]u8) {
305305 // march forward over whitespace
306306 while (true) : (self.index += 1) {
307307 const byte = self.cmd_line[self.index];
......@@ -355,8 +355,8 @@ pub const ArgIteratorWindows = struct {
355355 }
356356 }
357357
358 fn internalNext(self: *ArgIteratorWindows, allocator: *Allocator) NextError![]u8 {
359 var buf = std.ArrayList(u8).init(allocator);
358 fn internalNext(self: *ArgIteratorWindows, allocator: *Allocator) NextError![:0]u8 {
359 var buf = try std.ArrayListSentineled(u8, 0).init(allocator, "");
360360 defer buf.deinit();
361361
362362 var backslash_count: usize = 0;
......@@ -397,7 +397,7 @@ pub const ArgIteratorWindows = struct {
397397 }
398398 }
399399
400 fn emitBackslashes(self: *ArgIteratorWindows, buf: *std.ArrayList(u8), emit_count: usize) !void {
400 fn emitBackslashes(self: *ArgIteratorWindows, buf: *std.ArrayListSentineled(u8, 0), emit_count: usize) !void {
401401 var i: usize = 0;
402402 while (i < emit_count) : (i += 1) {
403403 try buf.append('\\');
......@@ -437,21 +437,21 @@ pub const ArgIterator = struct {
437437 pub const NextError = ArgIteratorWindows.NextError;
438438
439439 /// You must free the returned memory when done.
440 pub fn next(self: *ArgIterator, allocator: *Allocator) ?(NextError![]u8) {
440 pub fn next(self: *ArgIterator, allocator: *Allocator) ?(NextError![:0]u8) {
441441 if (builtin.os.tag == .windows) {
442442 return self.inner.next(allocator);
443443 } else {
444 return allocator.dupe(u8, self.inner.next() orelse return null);
444 return allocator.dupeZ(u8, self.inner.next() orelse return null);
445445 }
446446 }
447447
448448 /// If you only are targeting posix you can call this and not need an allocator.
449 pub fn nextPosix(self: *ArgIterator) ?[]const u8 {
449 pub fn nextPosix(self: *ArgIterator) ?[:0]const u8 {
450450 return self.inner.next();
451451 }
452452
453453 /// If you only are targeting WASI, you can call this and not need an allocator.
454 pub fn nextWasi(self: *ArgIterator) ?[]const u8 {
454 pub fn nextWasi(self: *ArgIterator) ?[:0]const u8 {
455455 return self.inner.next();
456456 }
457457
......@@ -501,7 +501,7 @@ test "args iterator" {
501501}
502502
503503/// Caller must call argsFree on result.
504pub fn argsAlloc(allocator: *mem.Allocator) ![][]u8 {
504pub fn argsAlloc(allocator: *mem.Allocator) ![][:0]u8 {
505505 // TODO refactor to only make 1 allocation.
506506 var it = if (builtin.os.tag == .wasi) try argsWithAllocator(allocator) else args();
507507 defer it.deinit();
......@@ -515,35 +515,36 @@ pub fn argsAlloc(allocator: *mem.Allocator) ![][]u8 {
515515 while (it.next(allocator)) |arg_or_err| {
516516 const arg = try arg_or_err;
517517 defer allocator.free(arg);
518 try contents.appendSlice(arg);
518 try contents.appendSlice(arg[0 .. arg.len + 1]);
519519 try slice_list.append(arg.len);
520520 }
521521
522522 const contents_slice = contents.span();
523523 const slice_sizes = slice_list.span();
524 const contents_size_bytes = try math.add(usize, contents_slice.len, slice_sizes.len);
524525 const slice_list_bytes = try math.mul(usize, @sizeOf([]u8), slice_sizes.len);
525 const total_bytes = try math.add(usize, slice_list_bytes, contents_slice.len);
526 const total_bytes = try math.add(usize, slice_list_bytes, contents_size_bytes);
526527 const buf = try allocator.alignedAlloc(u8, @alignOf([]u8), total_bytes);
527528 errdefer allocator.free(buf);
528529
529 const result_slice_list = mem.bytesAsSlice([]u8, buf[0..slice_list_bytes]);
530 const result_slice_list = mem.bytesAsSlice([:0]u8, buf[0..slice_list_bytes]);
530531 const result_contents = buf[slice_list_bytes..];
531532 mem.copy(u8, result_contents, contents_slice);
532533
533534 var contents_index: usize = 0;
534535 for (slice_sizes) |len, i| {
535536 const new_index = contents_index + len;
536 result_slice_list[i] = result_contents[contents_index..new_index];
537 contents_index = new_index;
537 result_slice_list[i] = result_contents[contents_index..new_index :0];
538 contents_index = new_index + 1;
538539 }
539540
540541 return result_slice_list;
541542}
542543
543pub fn argsFree(allocator: *mem.Allocator, args_alloc: []const []u8) void {
544pub fn argsFree(allocator: *mem.Allocator, args_alloc: []const [:0]u8) void {
544545 var total_bytes: usize = 0;
545546 for (args_alloc) |arg| {
546 total_bytes += @sizeOf([]u8) + arg.len;
547 total_bytes += @sizeOf([]u8) + arg.len + 1;
547548 }
548549 const unaligned_allocated_buf = @ptrCast([*]const u8, args_alloc.ptr)[0..total_bytes];
549550 const aligned_allocated_buf = @alignCast(@alignOf([]u8), unaligned_allocated_buf);