authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-23 21:01:54-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-01-23 21:01:54-05:00
logac28bedbeec5947a9de651175277b4e554334b16
treec7a246d6d29267aca035686ac0d76d548ec9c680
parentbbfbb7b22f8db1228e9ccd33f320bb30961ef911
parent186e8058381747b589190898560dcfa5622facc7
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #14418 from ifreund/assume-sentinel-sux

std: eliminate pointless meta.assumeSentinel() usage

8 files changed, 30 insertions(+), 24 deletions(-)

lib/std/debug.zig+1-1
...@@ -978,7 +978,7 @@ pub fn readElfDebugInfo(allocator: mem.Allocator, elf_file: File) !ModuleDebugIn...@@ -978,7 +978,7 @@ pub fn readElfDebugInfo(allocator: mem.Allocator, elf_file: File) !ModuleDebugIn
978 for (shdrs) |*shdr| {978 for (shdrs) |*shdr| {
979 if (shdr.sh_type == elf.SHT_NULL) continue;979 if (shdr.sh_type == elf.SHT_NULL) continue;
980980
981 const name = std.mem.span(std.meta.assumeSentinel(header_strings[shdr.sh_name..].ptr, 0));981 const name = mem.sliceTo(header_strings[shdr.sh_name..], 0);
982 if (mem.eql(u8, name, ".debug_info")) {982 if (mem.eql(u8, name, ".debug_info")) {
983 opt_debug_info = try chopSlice(mapped_mem, shdr.sh_offset, shdr.sh_size);983 opt_debug_info = try chopSlice(mapped_mem, shdr.sh_offset, shdr.sh_size);
984 } else if (mem.eql(u8, name, ".debug_abbrev")) {984 } else if (mem.eql(u8, name, ".debug_abbrev")) {
lib/std/fs.zig+2-2
...@@ -2968,14 +2968,14 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {...@@ -2968,14 +2968,14 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
2968 var out_len: usize = out_buffer.len;2968 var out_len: usize = out_buffer.len;
2969 try os.sysctl(&mib, out_buffer.ptr, &out_len, null, 0);2969 try os.sysctl(&mib, out_buffer.ptr, &out_len, null, 0);
2970 // TODO could this slice from 0 to out_len instead?2970 // TODO could this slice from 0 to out_len instead?
2971 return mem.sliceTo(std.meta.assumeSentinel(out_buffer.ptr, 0), 0);2971 return mem.sliceTo(out_buffer, 0);
2972 },2972 },
2973 .netbsd => {2973 .netbsd => {
2974 var mib = [4]c_int{ os.CTL.KERN, os.KERN.PROC_ARGS, -1, os.KERN.PROC_PATHNAME };2974 var mib = [4]c_int{ os.CTL.KERN, os.KERN.PROC_ARGS, -1, os.KERN.PROC_PATHNAME };
2975 var out_len: usize = out_buffer.len;2975 var out_len: usize = out_buffer.len;
2976 try os.sysctl(&mib, out_buffer.ptr, &out_len, null, 0);2976 try os.sysctl(&mib, out_buffer.ptr, &out_len, null, 0);
2977 // TODO could this slice from 0 to out_len instead?2977 // TODO could this slice from 0 to out_len instead?
2978 return mem.sliceTo(std.meta.assumeSentinel(out_buffer.ptr, 0), 0);2978 return mem.sliceTo(out_buffer, 0);
2979 },2979 },
2980 .openbsd, .haiku => {2980 .openbsd, .haiku => {
2981 // OpenBSD doesn't support getting the path of a running process, so try to guess it2981 // OpenBSD doesn't support getting the path of a running process, so try to guess it
lib/std/net.zig+14-6
...@@ -103,10 +103,10 @@ pub const Address = extern union {...@@ -103,10 +103,10 @@ pub const Address = extern union {
103 .path = undefined,103 .path = undefined,
104 };104 };
105105
106 // this enables us to have the proper length of the socket in getOsSockLen106 // Add 1 to ensure a terminating 0 is present in the path array for maximum portability.
107 mem.set(u8, &sock_addr.path, 0);107 if (path.len + 1 > sock_addr.path.len) return error.NameTooLong;
108108
109 if (path.len > sock_addr.path.len) return error.NameTooLong;109 mem.set(u8, &sock_addr.path, 0);
110 mem.copy(u8, &sock_addr.path, path);110 mem.copy(u8, &sock_addr.path, path);
111111
112 return Address{ .un = sock_addr };112 return Address{ .un = sock_addr };
...@@ -179,9 +179,17 @@ pub const Address = extern union {...@@ -179,9 +179,17 @@ pub const Address = extern union {
179 unreachable;179 unreachable;
180 }180 }
181181
182 const path_len = std.mem.len(std.meta.assumeSentinel(&self.un.path, 0));182 // Using the full length of the structure here is more portable than returning
183 return @intCast(os.socklen_t, @sizeOf(os.sockaddr.un) - self.un.path.len + path_len);183 // the number of bytes actually used by the currently stored path.
184 // This also is correct regardless if we are passing a socket address to the kernel
185 // (e.g. in bind, connect, sendto) since we ensure the path is 0 terminated in
186 // initUnix() or if we are receiving a socket address from the kernel and must
187 // provide the full buffer size (e.g. getsockname, getpeername, recvfrom, accept).
188 //
189 // To access the path, std.mem.sliceTo(&address.un.path, 0) should be used.
190 return @intCast(os.socklen_t, @sizeOf(os.sockaddr.un));
184 },191 },
192
185 else => unreachable,193 else => unreachable,
186 }194 }
187 }195 }
...@@ -1687,7 +1695,7 @@ fn dnsParseCallback(ctx: dpc_ctx, rr: u8, data: []const u8, packet: []const u8)...@@ -1687,7 +1695,7 @@ fn dnsParseCallback(ctx: dpc_ctx, rr: u8, data: []const u8, packet: []const u8)
1687 var tmp: [256]u8 = undefined;1695 var tmp: [256]u8 = undefined;
1688 // Returns len of compressed name. strlen to get canon name.1696 // Returns len of compressed name. strlen to get canon name.
1689 _ = try os.dn_expand(packet, data, &tmp);1697 _ = try os.dn_expand(packet, data, &tmp);
1690 const canon_name = mem.sliceTo(std.meta.assumeSentinel(&tmp, 0), 0);1698 const canon_name = mem.sliceTo(&tmp, 0);
1691 if (isValidHostName(canon_name)) {1699 if (isValidHostName(canon_name)) {
1692 ctx.canon.items.len = 0;1700 ctx.canon.items.len = 0;
1693 try ctx.canon.appendSlice(canon_name);1701 try ctx.canon.appendSlice(canon_name);
lib/std/os.zig+7-7
...@@ -1980,7 +1980,7 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {...@@ -1980,7 +1980,7 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {
1980 break :blk errno(system.getcwd(out_buffer.ptr, out_buffer.len));1980 break :blk errno(system.getcwd(out_buffer.ptr, out_buffer.len));
1981 };1981 };
1982 switch (err) {1982 switch (err) {
1983 .SUCCESS => return mem.sliceTo(std.meta.assumeSentinel(out_buffer.ptr, 0), 0),1983 .SUCCESS => return mem.sliceTo(out_buffer, 0),
1984 .FAULT => unreachable,1984 .FAULT => unreachable,
1985 .INVAL => unreachable,1985 .INVAL => unreachable,
1986 .NOENT => return error.CurrentWorkingDirectoryUnlinked,1986 .NOENT => return error.CurrentWorkingDirectoryUnlinked,
...@@ -5134,10 +5134,10 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {...@@ -5134,10 +5134,10 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
5134 return out_buffer[0..len];5134 return out_buffer[0..len];
5135 },5135 },
5136 .linux => {5136 .linux => {
5137 var procfs_buf: ["/proc/self/fd/-2147483648".len:0]u8 = undefined;5137 var procfs_buf: ["/proc/self/fd/-2147483648\x00".len]u8 = undefined;
5138 const proc_path = std.fmt.bufPrint(procfs_buf[0..], "/proc/self/fd/{d}\x00", .{fd}) catch unreachable;5138 const proc_path = std.fmt.bufPrintZ(procfs_buf[0..], "/proc/self/fd/{d}", .{fd}) catch unreachable;
51395139
5140 const target = readlinkZ(std.meta.assumeSentinel(proc_path.ptr, 0), out_buffer) catch |err| {5140 const target = readlinkZ(proc_path, out_buffer) catch |err| {
5141 switch (err) {5141 switch (err) {
5142 error.UnsupportedReparsePointType => unreachable, // Windows only,5142 error.UnsupportedReparsePointType => unreachable, // Windows only,
5143 error.NotLink => unreachable,5143 error.NotLink => unreachable,
...@@ -5147,7 +5147,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {...@@ -5147,7 +5147,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
5147 return target;5147 return target;
5148 },5148 },
5149 .solaris => {5149 .solaris => {
5150 var procfs_buf: ["/proc/self/path/-2147483648".len:0]u8 = undefined;5150 var procfs_buf: ["/proc/self/path/-2147483648\x00".len]u8 = undefined;
5151 const proc_path = std.fmt.bufPrintZ(procfs_buf[0..], "/proc/self/path/{d}", .{fd}) catch unreachable;5151 const proc_path = std.fmt.bufPrintZ(procfs_buf[0..], "/proc/self/path/{d}", .{fd}) catch unreachable;
51525152
5153 const target = readlinkZ(proc_path, out_buffer) catch |err| switch (err) {5153 const target = readlinkZ(proc_path, out_buffer) catch |err| switch (err) {
...@@ -5564,7 +5564,7 @@ pub const GetHostNameError = error{PermissionDenied} || UnexpectedError;...@@ -5564,7 +5564,7 @@ pub const GetHostNameError = error{PermissionDenied} || UnexpectedError;
5564pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {5564pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {
5565 if (builtin.link_libc) {5565 if (builtin.link_libc) {
5566 switch (errno(system.gethostname(name_buffer, name_buffer.len))) {5566 switch (errno(system.gethostname(name_buffer, name_buffer.len))) {
5567 .SUCCESS => return mem.sliceTo(std.meta.assumeSentinel(name_buffer, 0), 0),5567 .SUCCESS => return mem.sliceTo(name_buffer, 0),
5568 .FAULT => unreachable,5568 .FAULT => unreachable,
5569 .NAMETOOLONG => unreachable, // HOST_NAME_MAX prevents this5569 .NAMETOOLONG => unreachable, // HOST_NAME_MAX prevents this
5570 .PERM => return error.PermissionDenied,5570 .PERM => return error.PermissionDenied,
...@@ -5573,7 +5573,7 @@ pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {...@@ -5573,7 +5573,7 @@ pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {
5573 }5573 }
5574 if (builtin.os.tag == .linux) {5574 if (builtin.os.tag == .linux) {
5575 const uts = uname();5575 const uts = uname();
5576 const hostname = mem.sliceTo(std.meta.assumeSentinel(&uts.nodename, 0), 0);5576 const hostname = mem.sliceTo(&uts.nodename, 0);
5577 mem.copy(u8, name_buffer, hostname);5577 mem.copy(u8, name_buffer, hostname);
5578 return name_buffer[0..hostname.len];5578 return name_buffer[0..hostname.len];
5579 }5579 }
lib/std/os/windows.zig+1-1
...@@ -2010,7 +2010,7 @@ pub fn sliceToPrefixedFileW(s: []const u8) !PathSpace {...@@ -2010,7 +2010,7 @@ pub fn sliceToPrefixedFileW(s: []const u8) !PathSpace {
2010}2010}
20112011
2012fn getFullPathNameW(path: [*:0]const u16, out: []u16) !usize {2012fn getFullPathNameW(path: [*:0]const u16, out: []u16) !usize {
2013 const result = kernel32.GetFullPathNameW(path, @intCast(u32, out.len), std.meta.assumeSentinel(out.ptr, 0), null);2013 const result = kernel32.GetFullPathNameW(path, @intCast(u32, out.len), out.ptr, null);
2014 if (result == 0) {2014 if (result == 0) {
2015 switch (kernel32.GetLastError()) {2015 switch (kernel32.GetLastError()) {
2016 else => |err| return unexpectedError(err),2016 else => |err| return unexpectedError(err),
lib/std/os/windows/kernel32.zig+1-1
...@@ -223,7 +223,7 @@ pub extern "kernel32" fn GetFinalPathNameByHandleW(...@@ -223,7 +223,7 @@ pub extern "kernel32" fn GetFinalPathNameByHandleW(
223pub extern "kernel32" fn GetFullPathNameW(223pub extern "kernel32" fn GetFullPathNameW(
224 lpFileName: [*:0]const u16,224 lpFileName: [*:0]const u16,
225 nBufferLength: u32,225 nBufferLength: u32,
226 lpBuffer: ?[*:0]u16,226 lpBuffer: [*]u16,
227 lpFilePart: ?*?[*:0]u16,227 lpFilePart: ?*?[*:0]u16,
228) callconv(@import("std").os.windows.WINAPI) u32;228) callconv(@import("std").os.windows.WINAPI) u32;
229229
lib/std/pdb.zig+1-1
...@@ -671,7 +671,7 @@ pub const Pdb = struct {...@@ -671,7 +671,7 @@ pub const Pdb = struct {
671 const name_index = try reader.readIntLittle(u32);671 const name_index = try reader.readIntLittle(u32);
672 if (name_offset > name_bytes.len)672 if (name_offset > name_bytes.len)
673 return error.InvalidDebugInfo;673 return error.InvalidDebugInfo;
674 const name = mem.sliceTo(std.meta.assumeSentinel(name_bytes.ptr + name_offset, 0), 0);674 const name = mem.sliceTo(name_bytes[name_offset..], 0);
675 if (mem.eql(u8, name, "/names")) {675 if (mem.eql(u8, name, "/names")) {
676 break :str_tab_index name_index;676 break :str_tab_index name_index;
677 }677 }
lib/std/zig/system/NativeTargetInfo.zig+3-5
...@@ -533,8 +533,7 @@ fn glibcVerFromSoFile(file: fs.File) !std.builtin.Version {...@@ -533,8 +533,7 @@ fn glibcVerFromSoFile(file: fs.File) !std.builtin.Version {
533 @alignCast(@alignOf(elf.Elf64_Shdr), &sh_buf[sh_buf_i]),533 @alignCast(@alignOf(elf.Elf64_Shdr), &sh_buf[sh_buf_i]),
534 );534 );
535 const sh_name_off = elfInt(is_64, need_bswap, sh32.sh_name, sh64.sh_name);535 const sh_name_off = elfInt(is_64, need_bswap, sh32.sh_name, sh64.sh_name);
536 // TODO this pointer cast should not be necessary536 const sh_name = mem.sliceTo(shstrtab[sh_name_off..], 0);
537 const sh_name = mem.sliceTo(std.meta.assumeSentinel(shstrtab[sh_name_off..].ptr, 0), 0);
538 if (mem.eql(u8, sh_name, ".dynstr")) {537 if (mem.eql(u8, sh_name, ".dynstr")) {
539 break :find_dyn_str .{538 break :find_dyn_str .{
540 .offset = elfInt(is_64, need_bswap, sh32.sh_offset, sh64.sh_offset),539 .offset = elfInt(is_64, need_bswap, sh32.sh_offset, sh64.sh_offset),
...@@ -789,8 +788,7 @@ pub fn abiAndDynamicLinkerFromFile(...@@ -789,8 +788,7 @@ pub fn abiAndDynamicLinkerFromFile(
789 @alignCast(@alignOf(elf.Elf64_Shdr), &sh_buf[sh_buf_i]),788 @alignCast(@alignOf(elf.Elf64_Shdr), &sh_buf[sh_buf_i]),
790 );789 );
791 const sh_name_off = elfInt(is_64, need_bswap, sh32.sh_name, sh64.sh_name);790 const sh_name_off = elfInt(is_64, need_bswap, sh32.sh_name, sh64.sh_name);
792 // TODO this pointer cast should not be necessary791 const sh_name = mem.sliceTo(shstrtab[sh_name_off..], 0);
793 const sh_name = mem.sliceTo(std.meta.assumeSentinel(shstrtab[sh_name_off..].ptr, 0), 0);
794 if (mem.eql(u8, sh_name, ".dynstr")) {792 if (mem.eql(u8, sh_name, ".dynstr")) {
795 break :find_dyn_str .{793 break :find_dyn_str .{
796 .offset = elfInt(is_64, need_bswap, sh32.sh_offset, sh64.sh_offset),794 .offset = elfInt(is_64, need_bswap, sh32.sh_offset, sh64.sh_offset),
...@@ -812,7 +810,7 @@ pub fn abiAndDynamicLinkerFromFile(...@@ -812,7 +810,7 @@ pub fn abiAndDynamicLinkerFromFile(
812 const strtab_read_len = try preadMin(file, &strtab_buf, rpoff_file, strtab_len);810 const strtab_read_len = try preadMin(file, &strtab_buf, rpoff_file, strtab_len);
813 const strtab = strtab_buf[0..strtab_read_len];811 const strtab = strtab_buf[0..strtab_read_len];
814812
815 const rpath_list = mem.sliceTo(std.meta.assumeSentinel(strtab.ptr, 0), 0);813 const rpath_list = mem.sliceTo(strtab, 0);
816 var it = mem.tokenize(u8, rpath_list, ":");814 var it = mem.tokenize(u8, rpath_list, ":");
817 while (it.next()) |rpath| {815 while (it.next()) |rpath| {
818 if (glibcVerFromRPath(rpath)) |ver| {816 if (glibcVerFromRPath(rpath)) |ver| {