mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
std: eliminate pointless meta.assumeSentinel() usage
This fixes a bug in std.net caused during the introduction of meta.assumeSentinel due to the unfortunate semantics of mem.span() This leaves only 3 remaining uses of meta.assumeSentinel() in the standard library, each of which could be a simple @ptrCast([*:0]T, foo) instead. I think this function should likely be removed.
This commit is contained in:
parent
c0284e242f
commit
faf2fd18d3
8 changed files with 17 additions and 19 deletions
|
|
@ -978,7 +978,7 @@ pub fn readElfDebugInfo(allocator: mem.Allocator, elf_file: File) !ModuleDebugIn
|
|||
for (shdrs) |*shdr| {
|
||||
if (shdr.sh_type == elf.SHT_NULL) continue;
|
||||
|
||||
const name = std.mem.span(std.meta.assumeSentinel(header_strings[shdr.sh_name..].ptr, 0));
|
||||
const name = mem.sliceTo(header_strings[shdr.sh_name..], 0);
|
||||
if (mem.eql(u8, name, ".debug_info")) {
|
||||
opt_debug_info = try chopSlice(mapped_mem, shdr.sh_offset, shdr.sh_size);
|
||||
} else if (mem.eql(u8, name, ".debug_abbrev")) {
|
||||
|
|
|
|||
|
|
@ -2968,14 +2968,14 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
|
|||
var out_len: usize = out_buffer.len;
|
||||
try os.sysctl(&mib, out_buffer.ptr, &out_len, null, 0);
|
||||
// TODO could this slice from 0 to out_len instead?
|
||||
return mem.sliceTo(std.meta.assumeSentinel(out_buffer.ptr, 0), 0);
|
||||
return mem.sliceTo(out_buffer, 0);
|
||||
},
|
||||
.netbsd => {
|
||||
var mib = [4]c_int{ os.CTL.KERN, os.KERN.PROC_ARGS, -1, os.KERN.PROC_PATHNAME };
|
||||
var out_len: usize = out_buffer.len;
|
||||
try os.sysctl(&mib, out_buffer.ptr, &out_len, null, 0);
|
||||
// TODO could this slice from 0 to out_len instead?
|
||||
return mem.sliceTo(std.meta.assumeSentinel(out_buffer.ptr, 0), 0);
|
||||
return mem.sliceTo(out_buffer, 0);
|
||||
},
|
||||
.openbsd, .haiku => {
|
||||
// OpenBSD doesn't support getting the path of a running process, so try to guess it
|
||||
|
|
|
|||
|
|
@ -1687,7 +1687,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.sliceTo(std.meta.assumeSentinel(&tmp, 0), 0);
|
||||
const canon_name = mem.sliceTo(&tmp, 0);
|
||||
if (isValidHostName(canon_name)) {
|
||||
ctx.canon.items.len = 0;
|
||||
try ctx.canon.appendSlice(canon_name);
|
||||
|
|
|
|||
|
|
@ -1977,7 +1977,7 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {
|
|||
break :blk errno(system.getcwd(out_buffer.ptr, out_buffer.len));
|
||||
};
|
||||
switch (err) {
|
||||
.SUCCESS => return mem.sliceTo(std.meta.assumeSentinel(out_buffer.ptr, 0), 0),
|
||||
.SUCCESS => return mem.sliceTo(out_buffer, 0),
|
||||
.FAULT => unreachable,
|
||||
.INVAL => unreachable,
|
||||
.NOENT => return error.CurrentWorkingDirectoryUnlinked,
|
||||
|
|
@ -5131,10 +5131,10 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
|
|||
return out_buffer[0..len];
|
||||
},
|
||||
.linux => {
|
||||
var procfs_buf: ["/proc/self/fd/-2147483648".len:0]u8 = undefined;
|
||||
const proc_path = std.fmt.bufPrint(procfs_buf[0..], "/proc/self/fd/{d}\x00", .{fd}) catch unreachable;
|
||||
var procfs_buf: ["/proc/self/fd/-2147483648\x00".len]u8 = undefined;
|
||||
const proc_path = std.fmt.bufPrintZ(procfs_buf[0..], "/proc/self/fd/{d}", .{fd}) catch unreachable;
|
||||
|
||||
const target = readlinkZ(std.meta.assumeSentinel(proc_path.ptr, 0), out_buffer) catch |err| {
|
||||
const target = readlinkZ(proc_path, out_buffer) catch |err| {
|
||||
switch (err) {
|
||||
error.UnsupportedReparsePointType => unreachable, // Windows only,
|
||||
error.NotLink => unreachable,
|
||||
|
|
@ -5144,7 +5144,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
|
|||
return target;
|
||||
},
|
||||
.solaris => {
|
||||
var procfs_buf: ["/proc/self/path/-2147483648".len:0]u8 = undefined;
|
||||
var procfs_buf: ["/proc/self/path/-2147483648\x00".len]u8 = undefined;
|
||||
const proc_path = std.fmt.bufPrintZ(procfs_buf[0..], "/proc/self/path/{d}", .{fd}) catch unreachable;
|
||||
|
||||
const target = readlinkZ(proc_path, out_buffer) catch |err| switch (err) {
|
||||
|
|
@ -5561,7 +5561,7 @@ pub const GetHostNameError = error{PermissionDenied} || UnexpectedError;
|
|||
pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {
|
||||
if (builtin.link_libc) {
|
||||
switch (errno(system.gethostname(name_buffer, name_buffer.len))) {
|
||||
.SUCCESS => return mem.sliceTo(std.meta.assumeSentinel(name_buffer, 0), 0),
|
||||
.SUCCESS => return mem.sliceTo(name_buffer, 0),
|
||||
.FAULT => unreachable,
|
||||
.NAMETOOLONG => unreachable, // HOST_NAME_MAX prevents this
|
||||
.PERM => return error.PermissionDenied,
|
||||
|
|
@ -5570,7 +5570,7 @@ pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {
|
|||
}
|
||||
if (builtin.os.tag == .linux) {
|
||||
const uts = uname();
|
||||
const hostname = mem.sliceTo(std.meta.assumeSentinel(&uts.nodename, 0), 0);
|
||||
const hostname = mem.sliceTo(&uts.nodename, 0);
|
||||
mem.copy(u8, name_buffer, hostname);
|
||||
return name_buffer[0..hostname.len];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2006,7 +2006,7 @@ pub fn sliceToPrefixedFileW(s: []const u8) !PathSpace {
|
|||
}
|
||||
|
||||
fn getFullPathNameW(path: [*:0]const u16, out: []u16) !usize {
|
||||
const result = kernel32.GetFullPathNameW(path, @intCast(u32, out.len), std.meta.assumeSentinel(out.ptr, 0), null);
|
||||
const result = kernel32.GetFullPathNameW(path, @intCast(u32, out.len), out.ptr, null);
|
||||
if (result == 0) {
|
||||
switch (kernel32.GetLastError()) {
|
||||
else => |err| return unexpectedError(err),
|
||||
|
|
|
|||
|
|
@ -223,7 +223,7 @@ pub extern "kernel32" fn GetFinalPathNameByHandleW(
|
|||
pub extern "kernel32" fn GetFullPathNameW(
|
||||
lpFileName: [*:0]const u16,
|
||||
nBufferLength: u32,
|
||||
lpBuffer: ?[*:0]u16,
|
||||
lpBuffer: [*]u16,
|
||||
lpFilePart: ?*?[*:0]u16,
|
||||
) callconv(@import("std").os.windows.WINAPI) u32;
|
||||
|
||||
|
|
|
|||
|
|
@ -671,7 +671,7 @@ pub const Pdb = struct {
|
|||
const name_index = try reader.readIntLittle(u32);
|
||||
if (name_offset > name_bytes.len)
|
||||
return error.InvalidDebugInfo;
|
||||
const name = mem.sliceTo(std.meta.assumeSentinel(name_bytes.ptr + name_offset, 0), 0);
|
||||
const name = mem.sliceTo(name_bytes[name_offset..], 0);
|
||||
if (mem.eql(u8, name, "/names")) {
|
||||
break :str_tab_index name_index;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -533,8 +533,7 @@ fn glibcVerFromSoFile(file: fs.File) !std.builtin.Version {
|
|||
@alignCast(@alignOf(elf.Elf64_Shdr), &sh_buf[sh_buf_i]),
|
||||
);
|
||||
const sh_name_off = elfInt(is_64, need_bswap, sh32.sh_name, sh64.sh_name);
|
||||
// TODO this pointer cast should not be necessary
|
||||
const sh_name = mem.sliceTo(std.meta.assumeSentinel(shstrtab[sh_name_off..].ptr, 0), 0);
|
||||
const sh_name = mem.sliceTo(shstrtab[sh_name_off..], 0);
|
||||
if (mem.eql(u8, sh_name, ".dynstr")) {
|
||||
break :find_dyn_str .{
|
||||
.offset = elfInt(is_64, need_bswap, sh32.sh_offset, sh64.sh_offset),
|
||||
|
|
@ -789,8 +788,7 @@ pub fn abiAndDynamicLinkerFromFile(
|
|||
@alignCast(@alignOf(elf.Elf64_Shdr), &sh_buf[sh_buf_i]),
|
||||
);
|
||||
const sh_name_off = elfInt(is_64, need_bswap, sh32.sh_name, sh64.sh_name);
|
||||
// TODO this pointer cast should not be necessary
|
||||
const sh_name = mem.sliceTo(std.meta.assumeSentinel(shstrtab[sh_name_off..].ptr, 0), 0);
|
||||
const sh_name = mem.sliceTo(shstrtab[sh_name_off..], 0);
|
||||
if (mem.eql(u8, sh_name, ".dynstr")) {
|
||||
break :find_dyn_str .{
|
||||
.offset = elfInt(is_64, need_bswap, sh32.sh_offset, sh64.sh_offset),
|
||||
|
|
@ -812,7 +810,7 @@ pub fn abiAndDynamicLinkerFromFile(
|
|||
const strtab_read_len = try preadMin(file, &strtab_buf, rpoff_file, strtab_len);
|
||||
const strtab = strtab_buf[0..strtab_read_len];
|
||||
|
||||
const rpath_list = mem.sliceTo(std.meta.assumeSentinel(strtab.ptr, 0), 0);
|
||||
const rpath_list = mem.sliceTo(strtab, 0);
|
||||
var it = mem.tokenize(u8, rpath_list, ":");
|
||||
while (it.next()) |rpath| {
|
||||
if (glibcVerFromRPath(rpath)) |ver| {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue