authorgravatar for klingt.net@gmail.comAndreas Linz <klingt.net@gmail.com> 2020-12-23 10:16:27+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-12-23 11:16:27+02:00
log7e63f7ad0311c80bcec89649d723af8a97935ba2
treec5186540d15202aa105e1853bf14ef52d37f8eb0
parent4420dabdf5cb1ad421d62d946738eb61fcff0ce5
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Truncate user and group ids for 64 bit Linux systems (#7466)

* Truncate user and group ids 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! * Add a regression test for user and group ids * Replace @truncate with @intCast This should be safe because `uid_t` will be 32-bit. * Add missing import for getauxval * Add missing package names * Revert "Add missing import for getauxval" This reverts commit 38f93dc89effdf657f2b81a56b96527ce4083f52. * Skip user and group test if builtin.link_libc

2 files changed, 17 insertions(+), 8 deletions(-)

lib/std/os/linux.zig+8-8
......@@ -745,33 +745,33 @@ pub fn setregid(rgid: gid_t, egid: gid_t) usize {
745745
746746pub fn getuid() uid_t {
747747 if (@hasField(SYS, "getuid32")) {
748 return @as(uid_t, syscall0(.getuid32));
748 return @intCast(uid_t, syscall0(.getuid32));
749749 } else {
750 return @as(uid_t, syscall0(.getuid));
750 return @intCast(uid_t, syscall0(.getuid));
751751 }
752752}
753753
754754pub fn getgid() gid_t {
755755 if (@hasField(SYS, "getgid32")) {
756 return @as(gid_t, syscall0(.getgid32));
756 return @intCast(gid_t, syscall0(.getgid32));
757757 } else {
758 return @as(gid_t, syscall0(.getgid));
758 return @intCast(gid_t, syscall0(.getgid));
759759 }
760760}
761761
762762pub fn geteuid() uid_t {
763763 if (@hasField(SYS, "geteuid32")) {
764 return @as(uid_t, syscall0(.geteuid32));
764 return @intCast(uid_t, syscall0(.geteuid32));
765765 } else {
766 return @as(uid_t, syscall0(.geteuid));
766 return @intCast(uid_t, syscall0(.geteuid));
767767 }
768768}
769769
770770pub fn getegid() gid_t {
771771 if (@hasField(SYS, "getegid32")) {
772 return @as(gid_t, syscall0(.getegid32));
772 return @intCast(gid_t, syscall0(.getegid32));
773773 } else {
774 return @as(gid_t, syscall0(.getegid));
774 return @intCast(gid_t, syscall0(.getegid));
775775 }
776776}
777777
lib/std/os/linux/test.zig+9
......@@ -9,6 +9,7 @@ const linux = std.os.linux;
99const mem = std.mem;
1010const elf = std.elf;
1111const expect = std.testing.expect;
12const expectEqual = std.testing.expectEqual;
1213const fs = std.fs;
1314
1415test "fallocate" {
......@@ -99,3 +100,11 @@ test "statx" {
99100 expect(@bitCast(u64, @as(i64, stat_buf.blksize)) == statx_buf.blksize);
100101 expect(@bitCast(u64, @as(i64, stat_buf.blocks)) == statx_buf.blocks);
101102}
103
104test "user and group ids" {
105 if (builtin.link_libc) return error.SkipZigTest;
106 expectEqual(linux.getauxval(elf.AT_UID), linux.getuid());
107 expectEqual(linux.getauxval(elf.AT_GID), linux.getgid());
108 expectEqual(linux.getauxval(elf.AT_EUID), linux.geteuid());
109 expectEqual(linux.getauxval(elf.AT_EGID), linux.getegid());
110}