Skip to content

Commit

Permalink
make std.mem.toSlice use null terminated pointers
Browse files Browse the repository at this point in the history
and fix the fallout
  • Loading branch information
andrewrk committed Nov 25, 2019
1 parent 34b1ebe commit 15d415e
Show file tree
Hide file tree
Showing 17 changed files with 76 additions and 75 deletions.
9 changes: 2 additions & 7 deletions lib/std/buffer.zig
Expand Up @@ -72,11 +72,11 @@ pub const Buffer = struct {
self.list.deinit();
}

pub fn toSlice(self: Buffer) []u8 {
pub fn toSlice(self: Buffer) [:0]u8 {
return self.list.toSlice()[0..self.len()];
}

pub fn toSliceConst(self: Buffer) []const u8 {
pub fn toSliceConst(self: Buffer) [:0]const u8 {
return self.list.toSliceConst()[0..self.len()];
}

Expand Down Expand Up @@ -131,11 +131,6 @@ pub const Buffer = struct {
try self.resize(m.len);
mem.copy(u8, self.list.toSlice(), m);
}

/// For passing to C functions.
pub fn ptr(self: Buffer) [*]u8 {
return self.list.items.ptr;
}
};

test "simple Buffer" {
Expand Down
2 changes: 1 addition & 1 deletion lib/std/c.zig
Expand Up @@ -110,7 +110,7 @@ pub extern "c" fn nanosleep(rqtp: *const timespec, rmtp: ?*timespec) c_int;
pub extern "c" fn setreuid(ruid: c_uint, euid: c_uint) c_int;
pub extern "c" fn setregid(rgid: c_uint, egid: c_uint) c_int;
pub extern "c" fn rmdir(path: [*]const u8) c_int;
pub extern "c" fn getenv(name: [*]const u8) ?[*]u8;
pub extern "c" fn getenv(name: [*:0]const u8) ?[*:0]u8;
pub extern "c" fn sysctl(name: [*]const c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;
pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;
pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) c_int;
Expand Down
2 changes: 1 addition & 1 deletion lib/std/fs.zig
Expand Up @@ -533,7 +533,7 @@ pub const Dir = struct {
const next_index = self.index + linux_entry.reclen();
self.index = next_index;

const name = mem.toSlice(u8, @ptrCast([*]u8, &linux_entry.d_name));
const name = mem.toSlice(u8, @ptrCast([*:0]u8, &linux_entry.d_name));

// skip . and .. entries
if (mem.eql(u8, name, ".") or mem.eql(u8, name, "..")) {
Expand Down
6 changes: 3 additions & 3 deletions lib/std/mem.zig
Expand Up @@ -356,17 +356,17 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool {
return true;
}

pub fn len(comptime T: type, ptr: [*]const T) usize {
pub fn len(comptime T: type, ptr: [*:0]const T) usize {
var count: usize = 0;
while (ptr[count] != 0) : (count += 1) {}
return count;
}

pub fn toSliceConst(comptime T: type, ptr: [*]const T) []const T {
pub fn toSliceConst(comptime T: type, ptr: [*:0]const T) [:0]const T {
return ptr[0..len(T, ptr)];
}

pub fn toSlice(comptime T: type, ptr: [*]T) []T {
pub fn toSlice(comptime T: type, ptr: [*:0]T) [:0]T {
return ptr[0..len(T, ptr)];
}

Expand Down
4 changes: 2 additions & 2 deletions lib/std/net.zig
Expand Up @@ -360,7 +360,7 @@ pub const Address = extern union {
unreachable;
}

const path_len = std.mem.len(u8, &self.un.path);
const path_len = std.mem.len(u8, @ptrCast([*:0]const u8, &self.un.path));
return @intCast(os.socklen_t, @sizeOf(os.sockaddr_un) - self.un.path.len + path_len);
},
else => unreachable,
Expand Down Expand Up @@ -1271,7 +1271,7 @@ fn dnsParseCallback(ctx: dpc_ctx, rr: u8, data: []const u8, packet: []const u8)
var tmp: [256]u8 = undefined;
// Returns len of compressed name. strlen to get canon name.
_ = try os.dn_expand(packet, data, &tmp);
const canon_name = mem.toSliceConst(u8, &tmp);
const canon_name = mem.toSliceConst(u8, @ptrCast([*:0]const u8, &tmp));
if (isValidHostName(canon_name)) {
try ctx.canon.replaceContents(canon_name);
}
Expand Down
31 changes: 16 additions & 15 deletions lib/std/os.zig
Expand Up @@ -66,12 +66,12 @@ pub const system = if (builtin.link_libc) std.c else switch (builtin.os) {
pub usingnamespace @import("os/bits.zig");

/// See also `getenv`. Populated by startup code before main().
pub var environ: [][*]u8 = undefined;
pub var environ: [][*:0]u8 = undefined;

/// Populated by startup code before main().
/// Not available on Windows. See `std.process.args`
/// for obtaining the process arguments.
pub var argv: [][*]u8 = undefined;
pub var argv: [][*:0]u8 = undefined;

/// To obtain errno, call this function with the return value of the
/// system function call. For some systems this will obtain the value directly
Expand Down Expand Up @@ -784,7 +784,7 @@ pub fn execveC(path: [*]const u8, child_argv: [*]const ?[*]const u8, envp: [*]co
/// matching the syscall API on all targets. This removes the need for an allocator.
/// This function also uses the PATH environment variable to get the full path to the executable.
/// If `file` is an absolute path, this is the same as `execveC`.
pub fn execvpeC(file: [*]const u8, child_argv: [*]const ?[*]const u8, envp: [*]const ?[*]const u8) ExecveError {
pub fn execvpeC(file: [*:0]const u8, child_argv: [*]const ?[*:0]const u8, envp: [*]const ?[*:0]const u8) ExecveError {
const file_slice = mem.toSliceConst(u8, file);
if (mem.indexOfScalar(u8, file_slice, '/') != null) return execveC(file, child_argv, envp);

Expand Down Expand Up @@ -820,8 +820,8 @@ pub fn execvpe(
argv_slice: []const []const u8,
env_map: *const std.BufMap,
) (ExecveError || error{OutOfMemory}) {
const argv_buf = try allocator.alloc(?[*]u8, argv_slice.len + 1);
mem.set(?[*]u8, argv_buf, null);
const argv_buf = try allocator.alloc(?[*:0]u8, argv_slice.len + 1);
mem.set(?[*:0]u8, argv_buf, null);
defer {
for (argv_buf) |arg| {
const arg_buf = if (arg) |ptr| mem.toSlice(u8, ptr) else break;
Expand All @@ -834,7 +834,8 @@ pub fn execvpe(
@memcpy(arg_buf.ptr, arg.ptr, arg.len);
arg_buf[arg.len] = 0;

argv_buf[i] = arg_buf.ptr;
// TODO avoid @ptrCast using slice syntax with https://github.com/ziglang/zig/issues/3731
argv_buf[i] = @ptrCast([*:0]u8, arg_buf.ptr);
}
argv_buf[argv_slice.len] = null;

Expand All @@ -844,10 +845,10 @@ pub fn execvpe(
return execvpeC(argv_buf.ptr[0].?, argv_buf.ptr, envp_buf.ptr);
}

pub fn createNullDelimitedEnvMap(allocator: *mem.Allocator, env_map: *const std.BufMap) ![]?[*]u8 {
pub fn createNullDelimitedEnvMap(allocator: *mem.Allocator, env_map: *const std.BufMap) ![]?[*:0]u8 {
const envp_count = env_map.count();
const envp_buf = try allocator.alloc(?[*]u8, envp_count + 1);
mem.set(?[*]u8, envp_buf, null);
const envp_buf = try allocator.alloc(?[*:0]u8, envp_count + 1);
mem.set(?[*:0]u8, envp_buf, null);
errdefer freeNullDelimitedEnvMap(allocator, envp_buf);
{
var it = env_map.iterator();
Expand All @@ -859,15 +860,16 @@ pub fn createNullDelimitedEnvMap(allocator: *mem.Allocator, env_map: *const std.
@memcpy(env_buf.ptr + pair.key.len + 1, pair.value.ptr, pair.value.len);
env_buf[env_buf.len - 1] = 0;

envp_buf[i] = env_buf.ptr;
// TODO avoid @ptrCast using slice syntax with https://github.com/ziglang/zig/issues/3731
envp_buf[i] = @ptrCast([*:0]u8, env_buf.ptr);
}
assert(i == envp_count);
}
assert(envp_buf[envp_count] == null);
return envp_buf;
}

pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*]u8) void {
pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*:0]u8) void {
for (envp_buf) |env| {
const env_buf = if (env) |ptr| ptr[0 .. mem.len(u8, ptr) + 1] else break;
allocator.free(env_buf);
Expand Down Expand Up @@ -896,8 +898,7 @@ pub fn getenv(key: []const u8) ?[]const u8 {

/// Get an environment variable with a null-terminated name.
/// See also `getenv`.
/// TODO https://github.com/ziglang/zig/issues/265
pub fn getenvC(key: [*]const u8) ?[]const u8 {
pub fn getenvC(key: [*:0]const u8) ?[]const u8 {
if (builtin.link_libc) {
const value = system.getenv(key) orelse return null;
return mem.toSliceConst(u8, value);
Expand All @@ -922,7 +923,7 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {
break :blk errno(system.getcwd(out_buffer.ptr, out_buffer.len));
};
switch (err) {
0 => return mem.toSlice(u8, out_buffer.ptr),
0 => return mem.toSlice(u8, @ptrCast([*:0]u8, out_buffer.ptr)),
EFAULT => unreachable,
EINVAL => unreachable,
ENOENT => return error.CurrentWorkingDirectoryUnlinked,
Expand Down Expand Up @@ -2865,7 +2866,7 @@ pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {
var uts: utsname = undefined;
switch (errno(system.uname(&uts))) {
0 => {
const hostname = mem.toSlice(u8, &uts.nodename);
const hostname = mem.toSlice(u8, @ptrCast([*:0]u8, &uts.nodename));
mem.copy(u8, name_buffer, hostname);
return name_buffer[0..hostname.len];
},
Expand Down
6 changes: 4 additions & 2 deletions lib/std/os/linux/vdso.zig
Expand Up @@ -65,7 +65,8 @@ pub fn lookup(vername: []const u8, name: []const u8) usize {
if (0 == (@as(u32, 1) << @intCast(u5, syms[i].st_info & 0xf) & OK_TYPES)) continue;
if (0 == (@as(u32, 1) << @intCast(u5, syms[i].st_info >> 4) & OK_BINDS)) continue;
if (0 == syms[i].st_shndx) continue;
if (!mem.eql(u8, name, mem.toSliceConst(u8, strings + syms[i].st_name))) continue;
const sym_name = @ptrCast([*:0]const u8, strings + syms[i].st_name);
if (!mem.eql(u8, name, mem.toSliceConst(u8, sym_name))) continue;
if (maybe_versym) |versym| {
if (!checkver(maybe_verdef.?, versym[i], vername, strings))
continue;
Expand All @@ -87,5 +88,6 @@ fn checkver(def_arg: *elf.Verdef, vsym_arg: i32, vername: []const u8, strings: [
def = @intToPtr(*elf.Verdef, @ptrToInt(def) + def.vd_next);
}
const aux = @intToPtr(*elf.Verdaux, @ptrToInt(def) + def.vd_aux);
return mem.eql(u8, vername, mem.toSliceConst(u8, strings + aux.vda_name));
const vda_name = @ptrCast([*:0]const u8, strings + aux.vda_name);
return mem.eql(u8, vername, mem.toSliceConst(u8, vda_name));
}
12 changes: 6 additions & 6 deletions lib/std/special/start.zig
Expand Up @@ -123,12 +123,12 @@ fn posixCallMainAndExit() noreturn {
@setAlignStack(16);
}
const argc = starting_stack_ptr[0];
const argv = @ptrCast([*][*]u8, starting_stack_ptr + 1);
const argv = @ptrCast([*][*:0]u8, starting_stack_ptr + 1);

const envp_optional = @ptrCast([*]?[*]u8, argv + argc + 1);
const envp_optional = @ptrCast([*:null]?[*:0]u8, argv + argc + 1);
var envp_count: usize = 0;
while (envp_optional[envp_count]) |_| : (envp_count += 1) {}
const envp = @ptrCast([*][*]u8, envp_optional)[0..envp_count];
const envp = @ptrCast([*][*:0]u8, envp_optional)[0..envp_count];

if (builtin.os == .linux) {
// Find the beginning of the auxiliary vector
Expand Down Expand Up @@ -168,7 +168,7 @@ fn posixCallMainAndExit() noreturn {
std.os.exit(@inlineCall(callMainWithArgs, argc, argv, envp));
}

fn callMainWithArgs(argc: usize, argv: [*][*]u8, envp: [][*]u8) u8 {
fn callMainWithArgs(argc: usize, argv: [*][*:0]u8, envp: [][*:0]u8) u8 {
std.os.argv = argv[0..argc];
std.os.environ = envp;

Expand All @@ -177,10 +177,10 @@ fn callMainWithArgs(argc: usize, argv: [*][*]u8, envp: [][*]u8) u8 {
return initEventLoopAndCallMain();
}

extern fn main(c_argc: i32, c_argv: [*][*]u8, c_envp: [*]?[*]u8) i32 {
extern fn main(c_argc: i32, c_argv: [*][*:0]u8, c_envp: [*:null]?[*:0]u8) i32 {
var env_count: usize = 0;
while (c_envp[env_count] != null) : (env_count += 1) {}
const envp = @ptrCast([*][*]u8, c_envp)[0..env_count];
const envp = @ptrCast([*][*:0]u8, c_envp)[0..env_count];
return @inlineCall(callMainWithArgs, @intCast(usize, c_argc), c_argv, envp);
}

Expand Down
6 changes: 3 additions & 3 deletions src-self-hosted/clang.zig
Expand Up @@ -708,7 +708,7 @@ pub const ZigClangStringLiteral_StringKind = extern enum {
};

pub extern fn ZigClangSourceManager_getSpellingLoc(self: ?*const struct_ZigClangSourceManager, Loc: struct_ZigClangSourceLocation) struct_ZigClangSourceLocation;
pub extern fn ZigClangSourceManager_getFilename(self: *const struct_ZigClangSourceManager, SpellingLoc: struct_ZigClangSourceLocation) ?[*]const u8;
pub extern fn ZigClangSourceManager_getFilename(self: *const struct_ZigClangSourceManager, SpellingLoc: struct_ZigClangSourceLocation) ?[*:0]const u8;
pub extern fn ZigClangSourceManager_getSpellingLineNumber(self: ?*const struct_ZigClangSourceManager, Loc: struct_ZigClangSourceLocation) c_uint;
pub extern fn ZigClangSourceManager_getSpellingColumnNumber(self: ?*const struct_ZigClangSourceManager, Loc: struct_ZigClangSourceLocation) c_uint;
pub extern fn ZigClangSourceManager_getCharacterData(self: ?*const struct_ZigClangSourceManager, SL: struct_ZigClangSourceLocation) [*c]const u8;
Expand Down Expand Up @@ -746,7 +746,7 @@ pub extern fn ZigClangQualType_isRestrictQualified(self: struct_ZigClangQualType
pub extern fn ZigClangType_getTypeClass(self: ?*const struct_ZigClangType) ZigClangTypeClass;
pub extern fn ZigClangType_getPointeeType(self: ?*const struct_ZigClangType) struct_ZigClangQualType;
pub extern fn ZigClangType_isVoidType(self: ?*const struct_ZigClangType) bool;
pub extern fn ZigClangType_getTypeClassName(self: *const struct_ZigClangType) [*]const u8;
pub extern fn ZigClangType_getTypeClassName(self: *const struct_ZigClangType) [*:0]const u8;
pub extern fn ZigClangStmt_getBeginLoc(self: *const struct_ZigClangStmt) struct_ZigClangSourceLocation;
pub extern fn ZigClangStmt_getStmtClass(self: ?*const struct_ZigClangStmt) ZigClangStmtClass;
pub extern fn ZigClangStmt_classof_Expr(self: ?*const struct_ZigClangStmt) bool;
Expand Down Expand Up @@ -904,7 +904,7 @@ pub extern fn ZigClangLoadFromCommandLine(
) ?*ZigClangASTUnit;

pub extern fn ZigClangDecl_getKind(decl: *const ZigClangDecl) ZigClangDeclKind;
pub extern fn ZigClangDecl_getDeclKindName(decl: *const struct_ZigClangDecl) [*]const u8;
pub extern fn ZigClangDecl_getDeclKindName(decl: *const struct_ZigClangDecl) [*:0]const u8;

pub const ZigClangCompoundStmt_const_body_iterator = [*c]const *struct_ZigClangStmt;

Expand Down
6 changes: 3 additions & 3 deletions src-self-hosted/compilation.zig
Expand Up @@ -490,8 +490,8 @@ pub const Compilation = struct {
// LLVM creates invalid binaries on Windows sometimes.
// See https://github.com/ziglang/zig/issues/508
// As a workaround we do not use target native features on Windows.
var target_specific_cpu_args: ?[*]u8 = null;
var target_specific_cpu_features: ?[*]u8 = null;
var target_specific_cpu_args: ?[*:0]u8 = null;
var target_specific_cpu_features: ?[*:0]u8 = null;
defer llvm.DisposeMessage(target_specific_cpu_args);
defer llvm.DisposeMessage(target_specific_cpu_features);
if (target == Target.Native and !target.isWindows()) {
Expand All @@ -501,7 +501,7 @@ pub const Compilation = struct {

comp.target_machine = llvm.CreateTargetMachine(
comp.llvm_target,
comp.llvm_triple.ptr(),
comp.llvm_triple.toSliceConst(),
target_specific_cpu_args orelse "",
target_specific_cpu_features orelse "",
opt_level,
Expand Down

0 comments on commit 15d415e

Please sign in to comment.