| ... | ... | @@ -390,15 +390,8 @@ pub const ChildProcess = struct { |
| 390 | 390 | // can fail between fork() and execve(). |
| 391 | 391 | // Therefore, we do all the allocation for the execve() before the fork(). |
| 392 | 392 | // This means we must do the null-termination of argv and env vars here. |
| 393 | | const argv_buf = try arena.alloc(?[*:0]u8, self.argv.len + 1); |
| 394 | | for (self.argv) |arg, i| { |
| 395 | | const arg_buf = try arena.alloc(u8, arg.len + 1); |
| 396 | | @memcpy(arg_buf.ptr, arg.ptr, arg.len); |
| 397 | | arg_buf[arg.len] = 0; |
| 398 | | argv_buf[i] = arg_buf[0..arg.len :0].ptr; |
| 399 | | } |
| 400 | | argv_buf[self.argv.len] = null; |
| 401 | | const argv_ptr = argv_buf[0..self.argv.len :null].ptr; |
| 393 | const argv_buf = try arena.allocSentinel(?[*:0]u8, self.argv.len, null); |
| 394 | for (self.argv) |arg, i| argv_buf[i] = (try arena.dupeZ(u8, arg)).ptr; |
| 402 | 395 | |
| 403 | 396 | const envp = m: { |
| 404 | 397 | if (self.env_map) |env_map| { |
| ... | ... | @@ -465,8 +458,8 @@ pub const ChildProcess = struct { |
| 465 | 458 | } |
| 466 | 459 | |
| 467 | 460 | const err = switch (self.expand_arg0) { |
| 468 | | .expand => os.execvpeZ_expandArg0(.expand, argv_buf.ptr[0].?, argv_ptr, envp), |
| 469 | | .no_expand => os.execvpeZ_expandArg0(.no_expand, argv_buf.ptr[0].?, argv_ptr, envp), |
| 461 | .expand => os.execvpeZ_expandArg0(.expand, argv_buf.ptr[0].?, argv_buf.ptr, envp), |
| 462 | .no_expand => os.execvpeZ_expandArg0(.no_expand, argv_buf.ptr[0].?, argv_buf.ptr, envp), |
| 470 | 463 | }; |
| 471 | 464 | forkChildErrReport(err_pipe[1], err); |
| 472 | 465 | } |
| ... | ... | @@ -913,23 +906,20 @@ pub fn createWindowsEnvBlock(allocator: *mem.Allocator, env_map: *const BufMap) |
| 913 | 906 | |
| 914 | 907 | pub fn createNullDelimitedEnvMap(arena: *mem.Allocator, env_map: *const std.BufMap) ![:null]?[*:0]u8 { |
| 915 | 908 | const envp_count = env_map.count(); |
| 916 | | const envp_buf = try arena.alloc(?[*:0]u8, envp_count + 1); |
| 917 | | mem.set(?[*:0]u8, envp_buf, null); |
| 909 | const envp_buf = try arena.allocSentinel(?[*:0]u8, envp_count, null); |
| 918 | 910 | { |
| 919 | 911 | var it = env_map.iterator(); |
| 920 | 912 | var i: usize = 0; |
| 921 | 913 | while (it.next()) |pair| : (i += 1) { |
| 922 | | const env_buf = try arena.alloc(u8, pair.key.len + pair.value.len + 2); |
| 923 | | @memcpy(env_buf.ptr, pair.key.ptr, pair.key.len); |
| 914 | const env_buf = try arena.allocSentinel(u8, pair.key.len + pair.value.len + 1, 0); |
| 915 | mem.copy(u8, env_buf, pair.key); |
| 924 | 916 | env_buf[pair.key.len] = '='; |
| 925 | | @memcpy(env_buf.ptr + pair.key.len + 1, pair.value.ptr, pair.value.len); |
| 926 | | const len = env_buf.len - 1; |
| 927 | | env_buf[len] = 0; |
| 928 | | envp_buf[i] = env_buf[0..len :0].ptr; |
| 917 | mem.copy(u8, env_buf[pair.key.len + 1 ..], pair.value); |
| 918 | envp_buf[i] = env_buf.ptr; |
| 929 | 919 | } |
| 930 | 920 | assert(i == envp_count); |
| 931 | 921 | } |
| 932 | | return envp_buf[0..envp_count :null]; |
| 922 | return envp_buf; |
| 933 | 923 | } |
| 934 | 924 | |
| 935 | 925 | test "createNullDelimitedEnvMap" { |