Skip to content

Conversation

@vstinner
Copy link
Member

@vstinner vstinner commented Feb 25, 2022

@vstinner
Copy link
Member Author

Python 3.10 already uses similar code.

@methane: Would you mind to review this change?

} else {
n++; // Avoid malloc(0)
grouplist = PyMem_New(gid_t, n+1);
grouplist = PyMem_New(gid_t, n);
Copy link
Member

@methane methane Feb 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n+1 can be removed.

But I think n++ should be kept to avoid n = getgropus(0, grouplist) later.
When n=0, getgroups returns number of groups without changing grouplist.
Overallocating one entry is not so bad.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@methane It's easy to fix by surrounding:

n = getgroups(n, grouplist);
if (n == -1) {
    PyMem_Free(grouplist);
    return posix_error();
}

with: if (n > 0) { ... }.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I modified my PR to not call getgroups() if n=0. I also cleaned up the code to make it easier to follow.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arhadthedev: See my updated PR. I rewrote the code more it more "flat", easier to follow for the most common path.

@vstinner vstinner merged commit e02c475 into python:main Feb 26, 2022
@vstinner vstinner deleted the getgroup branch February 26, 2022 23:14
@vstinner
Copy link
Member Author

Thanks for the review @methane! The old code was easy to misunderstand. IMO having a special code path for n == 0 makes the code simpler to review but also more reliable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants