| author | |
| committer | |
| log | 7e63f7ad0311c80bcec89649d723af8a97935ba2 |
| tree | c5186540d15202aa105e1853bf14ef52d37f8eb0 |
| parent | 4420dabdf5cb1ad421d62d946738eb61fcff0ce5 |
| signature |
* 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_libc2 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 { | ... | @@ -745,33 +745,33 @@ pub fn setregid(rgid: gid_t, egid: gid_t) usize { |
| 745 | 745 | ||
| 746 | pub fn getuid() uid_t { | 746 | pub fn getuid() uid_t { |
| 747 | if (@hasField(SYS, "getuid32")) { | 747 | if (@hasField(SYS, "getuid32")) { |
| 748 | return @as(uid_t, syscall0(.getuid32)); | 748 | return @intCast(uid_t, syscall0(.getuid32)); |
| 749 | } else { | 749 | } else { |
| 750 | return @as(uid_t, syscall0(.getuid)); | 750 | return @intCast(uid_t, syscall0(.getuid)); |
| 751 | } | 751 | } |
| 752 | } | 752 | } |
| 753 | 753 | ||
| 754 | pub fn getgid() gid_t { | 754 | pub fn getgid() gid_t { |
| 755 | if (@hasField(SYS, "getgid32")) { | 755 | if (@hasField(SYS, "getgid32")) { |
| 756 | return @as(gid_t, syscall0(.getgid32)); | 756 | return @intCast(gid_t, syscall0(.getgid32)); |
| 757 | } else { | 757 | } else { |
| 758 | return @as(gid_t, syscall0(.getgid)); | 758 | return @intCast(gid_t, syscall0(.getgid)); |
| 759 | } | 759 | } |
| 760 | } | 760 | } |
| 761 | 761 | ||
| 762 | pub fn geteuid() uid_t { | 762 | pub fn geteuid() uid_t { |
| 763 | if (@hasField(SYS, "geteuid32")) { | 763 | if (@hasField(SYS, "geteuid32")) { |
| 764 | return @as(uid_t, syscall0(.geteuid32)); | 764 | return @intCast(uid_t, syscall0(.geteuid32)); |
| 765 | } else { | 765 | } else { |
| 766 | return @as(uid_t, syscall0(.geteuid)); | 766 | return @intCast(uid_t, syscall0(.geteuid)); |
| 767 | } | 767 | } |
| 768 | } | 768 | } |
| 769 | 769 | ||
| 770 | pub fn getegid() gid_t { | 770 | pub fn getegid() gid_t { |
| 771 | if (@hasField(SYS, "getegid32")) { | 771 | if (@hasField(SYS, "getegid32")) { |
| 772 | return @as(gid_t, syscall0(.getegid32)); | 772 | return @intCast(gid_t, syscall0(.getegid32)); |
| 773 | } else { | 773 | } else { |
| 774 | return @as(gid_t, syscall0(.getegid)); | 774 | return @intCast(gid_t, syscall0(.getegid)); |
| 775 | } | 775 | } |
| 776 | } | 776 | } |
| 777 | 777 |
lib/std/os/linux/test.zig+9| ... | @@ -9,6 +9,7 @@ const linux = std.os.linux; | ... | @@ -9,6 +9,7 @@ const linux = std.os.linux; |
| 9 | const mem = std.mem; | 9 | const mem = std.mem; |
| 10 | const elf = std.elf; | 10 | const elf = std.elf; |
| 11 | const expect = std.testing.expect; | 11 | const expect = std.testing.expect; |
| 12 | const expectEqual = std.testing.expectEqual; | ||
| 12 | const fs = std.fs; | 13 | const fs = std.fs; |
| 13 | 14 | ||
| 14 | test "fallocate" { | 15 | test "fallocate" { |
| ... | @@ -99,3 +100,11 @@ test "statx" { | ... | @@ -99,3 +100,11 @@ test "statx" { |
| 99 | expect(@bitCast(u64, @as(i64, stat_buf.blksize)) == statx_buf.blksize); | 100 | expect(@bitCast(u64, @as(i64, stat_buf.blksize)) == statx_buf.blksize); |
| 100 | expect(@bitCast(u64, @as(i64, stat_buf.blocks)) == statx_buf.blocks); | 101 | expect(@bitCast(u64, @as(i64, stat_buf.blocks)) == statx_buf.blocks); |
| 101 | } | 102 | } |
| 103 | |||
| 104 | test "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 | } |