Skip to content

sysctlbynameZ does not return NameTooLong, drop dead prongs #24909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/std/Thread.zig
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ const PosixThreadImpl = struct {
var count_len: usize = @sizeOf(c_int);
const name = if (comptime target.os.tag.isDarwin()) "hw.logicalcpu" else "hw.ncpu";
posix.sysctlbynameZ(name, &count, &count_len, null, 0) catch |err| switch (err) {
error.NameTooLong, error.UnknownName => unreachable,
error.UnknownName => unreachable,
else => |e| return e,
};
return @as(usize, @intCast(count));
Expand Down
8 changes: 7 additions & 1 deletion lib/std/posix.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5219,13 +5219,19 @@ pub fn sysctl(
}
}

pub const SysCtlByNameError = error{
PermissionDenied,
SystemResources,
UnknownName,
} || UnexpectedError;

pub fn sysctlbynameZ(
name: [*:0]const u8,
oldp: ?*anyopaque,
oldlenp: ?*usize,
newp: ?*anyopaque,
newlen: usize,
) SysCtlError!void {
) SysCtlByNameError!void {
if (native_os == .wasi) {
@compileError("sysctl not supported on WASI");
}
Expand Down
2 changes: 1 addition & 1 deletion lib/std/process.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1759,7 +1759,7 @@ pub fn totalSystemMemory() TotalSystemMemoryError!u64 {
var physmem: c_ulong = undefined;
var len: usize = @sizeOf(c_ulong);
posix.sysctlbynameZ("hw.physmem", &physmem, &len, null, 0) catch |err| switch (err) {
error.NameTooLong, error.UnknownName => unreachable,
error.UnknownName => unreachable,
else => return error.UnknownTotalSystemMemory,
};
return @as(usize, @intCast(physmem));
Expand Down
1 change: 0 additions & 1 deletion lib/std/zig/system.zig
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ pub fn resolveTargetQuery(query: Target.Query) DetectError!Target {
var len: usize = @sizeOf(@TypeOf(value));

posix.sysctlbynameZ(key, &value, &len, null, 0) catch |err| switch (err) {
error.NameTooLong => unreachable, // constant, known good value
error.PermissionDenied => unreachable, // only when setting values,
error.SystemResources => unreachable, // memory already on the stack
error.UnknownName => unreachable, // constant, known good value
Expand Down
1 change: 0 additions & 1 deletion lib/std/zig/system/darwin/macos.zig
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ pub fn detectNativeCpuAndFeatures() ?Target.Cpu {
var cpu_family: std.c.CPUFAMILY = undefined;
var len: usize = @sizeOf(std.c.CPUFAMILY);
std.posix.sysctlbynameZ("hw.cpufamily", &cpu_family, &len, null, 0) catch |err| switch (err) {
error.NameTooLong => unreachable, // constant, known good value
error.PermissionDenied => unreachable, // only when setting values,
error.SystemResources => unreachable, // memory already on the stack
error.UnknownName => unreachable, // constant, known good value
Expand Down