Skip to content
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

fix: allow std.os.linux.getgroups to accept null #22286

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sweetbbak
Copy link

looking at man getgroups and info getgroups this is given as an example:

     // 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. However this is not possible with the function signature getgroups(size: usize, list: *gid_t) usize where "list" is supposedly a pointer to the beginning of a list of unknown length.

equivalent example in Zig after the change:

  // 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));

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));
  ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants