diff --git a/sys/kern/kern_prot.c b/sys/kern/kern_prot.c --- a/sys/kern/kern_prot.c +++ b/sys/kern/kern_prot.c @@ -50,6 +50,7 @@ #include #include #include +#include #include #include #include @@ -832,6 +833,15 @@ return (error); } +static int +gidp_cmp(const void *p1, const void *p2) +{ + const gid_t g1 = *(const gid_t *)p1; + const gid_t g2 = *(const gid_t *)p2; + + return ((g1 > g2) - (g1 < g2)); +} + int kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups) { @@ -1282,24 +1292,13 @@ static bool group_is_supplementary(const gid_t gid, const struct ucred *const cred) { - int l, h, m; /* * Perform a binary search of the supplementary groups. This is * possible because we sort the groups in crsetgroups(). */ - l = 1; - h = cred->cr_ngroups; - - while (l < h) { - m = l + (h - l) / 2; - if (cred->cr_groups[m] < gid) - l = m + 1; - else - h = m; - } - - return (l < cred->cr_ngroups && cred->cr_groups[l] == gid); + return (bsearch(&gid, cred->cr_groups + 1, cred->cr_ngroups - 1, + sizeof(gid), gidp_cmp) != NULL); } /*