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
978978 for (shdrs) |*shdr| {
979979 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);
982982 if (mem.eql(u8, name, ".debug_info")) {
983983 opt_debug_info = try chopSlice(mapped_mem, shdr.sh_offset, shdr.sh_size);
984984 } 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 {
29682968 var out_len: usize = out_buffer.len;
29692969 try os.sysctl(&mib, out_buffer.ptr, &out_len, null, 0);
29702970 // 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);
29722972 },
29732973 .netbsd => {
29742974 var mib = [4]c_int{ os.CTL.KERN, os.KERN.PROC_ARGS, -1, os.KERN.PROC_PATHNAME };
29752975 var out_len: usize = out_buffer.len;
29762976 try os.sysctl(&mib, out_buffer.ptr, &out_len, null, 0);
29772977 // 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);
29792979 },
29802980 .openbsd, .haiku => {
29812981 // 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 {
103103 .path = undefined,
104104 };
105105
106 // this enables us to have the proper length of the socket in getOsSockLen
107 mem.set(u8, &sock_addr.path, 0);
106 // Add 1 to ensure a terminating 0 is present in the path array for maximum portability.
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);
110110 mem.copy(u8, &sock_addr.path, path);
111111
112112 return Address{ .un = sock_addr };
......@@ -179,9 +179,17 @@ pub const Address = extern union {
179179 unreachable;
180180 }
181181
182 const path_len = std.mem.len(std.meta.assumeSentinel(&self.un.path, 0));
183 return @intCast(os.socklen_t, @sizeOf(os.sockaddr.un) - self.un.path.len + path_len);
182 // Using the full length of the structure here is more portable than returning
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));
184191 },
192
185193 else => unreachable,
186194 }
187195 }
......@@ -1687,7 +1695,7 @@ fn dnsParseCallback(ctx: dpc_ctx, rr: u8, data: []const u8, packet: []const u8)
16871695 var tmp: [256]u8 = undefined;
16881696 // Returns len of compressed name. strlen to get canon name.
16891697 _ = 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);
16911699 if (isValidHostName(canon_name)) {
16921700 ctx.canon.items.len = 0;
16931701 try ctx.canon.appendSlice(canon_name);
lib/std/os.zig+7-7
......@@ -1980,7 +1980,7 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {
19801980 break :blk errno(system.getcwd(out_buffer.ptr, out_buffer.len));
19811981 };
19821982 switch (err) {
1983 .SUCCESS => return mem.sliceTo(std.meta.assumeSentinel(out_buffer.ptr, 0), 0),
1983 .SUCCESS => return mem.sliceTo(out_buffer, 0),
19841984 .FAULT => unreachable,
19851985 .INVAL => unreachable,
19861986 .NOENT => return error.CurrentWorkingDirectoryUnlinked,
......@@ -5134,10 +5134,10 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
51345134 return out_buffer[0..len];
51355135 },
51365136 .linux => {
5137 var procfs_buf: ["/proc/self/fd/-2147483648".len:0]u8 = undefined;
5138 const proc_path = std.fmt.bufPrint(procfs_buf[0..], "/proc/self/fd/{d}\x00", .{fd}) catch unreachable;
5137 var procfs_buf: ["/proc/self/fd/-2147483648\x00".len]u8 = undefined;
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| {
51415141 switch (err) {
51425142 error.UnsupportedReparsePointType => unreachable, // Windows only,
51435143 error.NotLink => unreachable,
......@@ -5147,7 +5147,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
51475147 return target;
51485148 },
51495149 .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;
51515151 const proc_path = std.fmt.bufPrintZ(procfs_buf[0..], "/proc/self/path/{d}", .{fd}) catch unreachable;
51525152
51535153 const target = readlinkZ(proc_path, out_buffer) catch |err| switch (err) {
......@@ -5564,7 +5564,7 @@ pub const GetHostNameError = error{PermissionDenied} || UnexpectedError;
55645564pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {
55655565 if (builtin.link_libc) {
55665566 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),
55685568 .FAULT => unreachable,
55695569 .NAMETOOLONG => unreachable, // HOST_NAME_MAX prevents this
55705570 .PERM => return error.PermissionDenied,
......@@ -5573,7 +5573,7 @@ pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {
55735573 }
55745574 if (builtin.os.tag == .linux) {
55755575 const uts = uname();
5576 const hostname = mem.sliceTo(std.meta.assumeSentinel(&uts.nodename, 0), 0);
5576 const hostname = mem.sliceTo(&uts.nodename, 0);
55775577 mem.copy(u8, name_buffer, hostname);
55785578 return name_buffer[0..hostname.len];
55795579 }
lib/std/os/windows.zig+1-1
......@@ -2010,7 +2010,7 @@ pub fn sliceToPrefixedFileW(s: []const u8) !PathSpace {
20102010}
20112011
20122012fn 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);
20142014 if (result == 0) {
20152015 switch (kernel32.GetLastError()) {
20162016 else => |err| return unexpectedError(err),
lib/std/os/windows/kernel32.zig+1-1
......@@ -223,7 +223,7 @@ pub extern "kernel32" fn GetFinalPathNameByHandleW(
223223pub extern "kernel32" fn GetFullPathNameW(
224224 lpFileName: [*:0]const u16,
225225 nBufferLength: u32,
226 lpBuffer: ?[*:0]u16,
226 lpBuffer: [*]u16,
227227 lpFilePart: ?*?[*:0]u16,
228228) callconv(@import("std").os.windows.WINAPI) u32;
229229
lib/std/pdb.zig+1-1
......@@ -671,7 +671,7 @@ pub const Pdb = struct {
671671 const name_index = try reader.readIntLittle(u32);
672672 if (name_offset > name_bytes.len)
673673 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);
675675 if (mem.eql(u8, name, "/names")) {
676676 break :str_tab_index name_index;
677677 }
lib/std/zig/system/NativeTargetInfo.zig+3-5
......@@ -533,8 +533,7 @@ fn glibcVerFromSoFile(file: fs.File) !std.builtin.Version {
533533 @alignCast(@alignOf(elf.Elf64_Shdr), &sh_buf[sh_buf_i]),
534534 );
535535 const sh_name_off = elfInt(is_64, need_bswap, sh32.sh_name, sh64.sh_name);
536 // TODO this pointer cast should not be necessary
537 const sh_name = mem.sliceTo(std.meta.assumeSentinel(shstrtab[sh_name_off..].ptr, 0), 0);
536 const sh_name = mem.sliceTo(shstrtab[sh_name_off..], 0);
538537 if (mem.eql(u8, sh_name, ".dynstr")) {
539538 break :find_dyn_str .{
540539 .offset = elfInt(is_64, need_bswap, sh32.sh_offset, sh64.sh_offset),
......@@ -789,8 +788,7 @@ pub fn abiAndDynamicLinkerFromFile(
789788 @alignCast(@alignOf(elf.Elf64_Shdr), &sh_buf[sh_buf_i]),
790789 );
791790 const sh_name_off = elfInt(is_64, need_bswap, sh32.sh_name, sh64.sh_name);
792 // TODO this pointer cast should not be necessary
793 const sh_name = mem.sliceTo(std.meta.assumeSentinel(shstrtab[sh_name_off..].ptr, 0), 0);
791 const sh_name = mem.sliceTo(shstrtab[sh_name_off..], 0);
794792 if (mem.eql(u8, sh_name, ".dynstr")) {
795793 break :find_dyn_str .{
796794 .offset = elfInt(is_64, need_bswap, sh32.sh_offset, sh64.sh_offset),
......@@ -812,7 +810,7 @@ pub fn abiAndDynamicLinkerFromFile(
812810 const strtab_read_len = try preadMin(file, &strtab_buf, rpoff_file, strtab_len);
813811 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);
816814 var it = mem.tokenize(u8, rpath_list, ":");
817815 while (it.next()) |rpath| {
818816 if (glibcVerFromRPath(rpath)) |ver| {