From 9cd07e4cda12b5940b56d0d77ad1edf5069c62cb Mon Sep 17 00:00:00 2001 From: Meili C Date: Sat, 21 Dec 2024 15:42:28 -0900 Subject: [PATCH] fix: allow std.linux.getgroups to accept null MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit looking at `man getgroups` and `info getgroups` this is given as an example: ```c // Here's how to use ‘getgroups’ to read all the supplementary group // IDs: gid_t * read_all_groups (void) { int ngroups = getgroups (0, NULL); gid_t *groups = (gid_t *) xmalloc (ngroups * sizeof (gid_t)); int val = getgroups (ngroups, groups); if (val < 0) { free (groups); return NULL; } return groups; } ``` getgroups(0, NULL) is used to get the count of groups so that the correct count can be used to allocate a list of gid_t. This small changes makes this possible. equivalent example in Zig after the change: ```zig // get the group count const ngroups: usize = std.os.linux.getgroups(0, null); if (ngroups <= 0) { return error.GetGroupsError; } std.debug.print("number of groups: {d}\n", .{ngroups}); const groups_gids: []u32 = try alloc.alloc(u32, ngroups); // populate an array of gid_t _ = std.os.linux.getgroups(ngroups, @ptrCast(groups_gids)); ``` --- lib/std/os/linux.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index e9a84d806256..d8a597ad0d62 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -1674,7 +1674,7 @@ pub fn setpgid(pid: pid_t, pgid: pid_t) usize { return syscall2(.setpgid, @intCast(pid), @intCast(pgid)); } -pub fn getgroups(size: usize, list: *gid_t) usize { +pub fn getgroups(size: usize, list: ?*gid_t) usize { if (@hasField(SYS, "getgroups32")) { return syscall2(.getgroups32, size, @intFromPtr(list)); } else {