Skip to content

Commit

Permalink
Truncate user and group ids
Browse files Browse the repository at this point in the history
Calls to `getuid`, `getgid` and their `eid` variants fail to compile on
64bit Linux systems because the return value of the syscall is of
`usize` and needs to be truncated to fit the size of `uid_t` that is 32
bit.

Thanks to @FireFox317 for figuring this out in Zig's Discord channel!
  • Loading branch information
Andreas Linz committed Dec 16, 2020
1 parent d877eb0 commit b294e14
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/std/os/linux.zig
Original file line number Diff line number Diff line change
Expand Up @@ -747,31 +747,31 @@ pub fn getuid() uid_t {
if (@hasField(SYS, "getuid32")) {
return @as(uid_t, syscall0(.getuid32));
} else {
return @as(uid_t, syscall0(.getuid));
return @truncate(uid_t, syscall0(.getuid));
}
}

pub fn getgid() gid_t {
if (@hasField(SYS, "getgid32")) {
return @as(gid_t, syscall0(.getgid32));
} else {
return @as(gid_t, syscall0(.getgid));
return @truncate(gid_t, syscall0(.getgid));
}
}

pub fn geteuid() uid_t {
if (@hasField(SYS, "geteuid32")) {
return @as(uid_t, syscall0(.geteuid32));
} else {
return @as(uid_t, syscall0(.geteuid));
return @truncate(uid_t, syscall0(.geteuid));
}
}

pub fn getegid() gid_t {
if (@hasField(SYS, "getegid32")) {
return @as(gid_t, syscall0(.getegid32));
} else {
return @as(gid_t, syscall0(.getegid));
return @truncate(gid_t, syscall0(.getegid));
}
}

Expand Down

0 comments on commit b294e14

Please sign in to comment.