aboutsummaryrefslogtreecommitdiff
path: root/lib/std/os.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2019-12-18 16:43:56 -0500
committerAndrew Kelley <andrew@ziglang.org>2019-12-20 18:28:56 -0500
commit26f3c2d0614f4fb37752b1931cb0b43aed2696d2 (patch)
tree18e51d6ed267ded8c6d87b5158c8866dcf1a2bf6 /lib/std/os.zig
parent51cbd968203f348051b8c2bdc005ca5294a79ceb (diff)
downloadzig-26f3c2d0614f4fb37752b1931cb0b43aed2696d2.tar.gz
zig-26f3c2d0614f4fb37752b1931cb0b43aed2696d2.zip
fix std.mem.addNullByte and implement sentinel slicing
see #3770
Diffstat (limited to 'lib/std/os.zig')
-rw-r--r--lib/std/os.zig25
1 files changed, 9 insertions, 16 deletions
diff --git a/lib/std/os.zig b/lib/std/os.zig
index 6d49bcd38e..3cc43357bd 100644
--- a/lib/std/os.zig
+++ b/lib/std/os.zig
@@ -805,9 +805,9 @@ pub fn execvpeC(file: [*:0]const u8, child_argv: [*:null]const ?[*:0]const u8, e
mem.copy(u8, &path_buf, search_path);
path_buf[search_path.len] = '/';
mem.copy(u8, path_buf[search_path.len + 1 ..], file_slice);
- path_buf[search_path.len + file_slice.len + 1] = 0;
- // TODO avoid @ptrCast here using slice syntax with https://github.com/ziglang/zig/issues/3770
- err = execveC(@ptrCast([*:0]u8, &path_buf), child_argv, envp);
+ const path_len = search_path.len + file_slice.len + 1;
+ path_buf[path_len] = 0;
+ err = execveC(path_buf[0..path_len :0].ptr, child_argv, envp);
switch (err) {
error.AccessDenied => seen_eacces = true,
error.FileNotFound, error.NotDir => {},
@@ -841,18 +841,14 @@ pub fn execvpe(
const arg_buf = try allocator.alloc(u8, arg.len + 1);
@memcpy(arg_buf.ptr, arg.ptr, arg.len);
arg_buf[arg.len] = 0;
-
- // TODO avoid @ptrCast using slice syntax with https://github.com/ziglang/zig/issues/3770
- argv_buf[i] = @ptrCast([*:0]u8, arg_buf.ptr);
+ argv_buf[i] = arg_buf[0..arg.len :0].ptr;
}
argv_buf[argv_slice.len] = null;
+ const argv_ptr = argv_buf[0..argv_slice.len :null].ptr;
const envp_buf = try createNullDelimitedEnvMap(allocator, env_map);
defer freeNullDelimitedEnvMap(allocator, envp_buf);
- // TODO avoid @ptrCast here using slice syntax with https://github.com/ziglang/zig/issues/3770
- const argv_ptr = @ptrCast([*:null]?[*:0]u8, argv_buf.ptr);
-
return execvpeC(argv_buf.ptr[0].?, argv_ptr, envp_buf.ptr);
}
@@ -869,16 +865,13 @@ pub fn createNullDelimitedEnvMap(allocator: *mem.Allocator, env_map: *const std.
@memcpy(env_buf.ptr, pair.key.ptr, pair.key.len);
env_buf[pair.key.len] = '=';
@memcpy(env_buf.ptr + pair.key.len + 1, pair.value.ptr, pair.value.len);
- env_buf[env_buf.len - 1] = 0;
-
- // TODO avoid @ptrCast here using slice syntax with https://github.com/ziglang/zig/issues/3770
- envp_buf[i] = @ptrCast([*:0]u8, env_buf.ptr);
+ const len = env_buf.len - 1;
+ env_buf[len] = 0;
+ envp_buf[i] = env_buf[0..len :0].ptr;
}
assert(i == envp_count);
}
- // TODO avoid @ptrCast here using slice syntax with https://github.com/ziglang/zig/issues/3770
- assert(envp_buf[envp_count] == null);
- return @ptrCast([*:null]?[*:0]u8, envp_buf.ptr)[0..envp_count];
+ return envp_buf[0..envp_count :null];
}
pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*:0]u8) void {