git: 372605478c6f - main - cred: group_is_supplementary(): Use bsearch()
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Sat, 02 Nov 2024 20:39:27 UTC
The branch main has been updated by olce:
URL: https://cgit.FreeBSD.org/src/commit/?id=372605478c6fe2c628f25428af201f866d7eb015
commit 372605478c6fe2c628f25428af201f866d7eb015
Author: Olivier Certner <olce@FreeBSD.org>
AuthorDate: 2024-07-19 11:23:19 +0000
Commit: Olivier Certner <olce@FreeBSD.org>
CommitDate: 2024-11-02 20:37:40 +0000
cred: group_is_supplementary(): Use bsearch()
This makes that function use a more efficient version of binary search
instead, and removes one more hand-rolled binary search code from the
tree (and the kernel binary).
Reviewed by: mhorne, emaste
Approved by: markj (mentor)
MFC after: 3 days
Differential Revision: https://reviews.freebsd.org/D46907
---
sys/kern/kern_prot.c | 25 ++++++++++++-------------
1 file changed, 12 insertions(+), 13 deletions(-)
diff --git a/sys/kern/kern_prot.c b/sys/kern/kern_prot.c
index b2511ce1e6c8..5ba5afc52915 100644
--- a/sys/kern/kern_prot.c
+++ b/sys/kern/kern_prot.c
@@ -50,6 +50,7 @@
#include <sys/acct.h>
#include <sys/kdb.h>
#include <sys/kernel.h>
+#include <sys/libkern.h>
#include <sys/lock.h>
#include <sys/loginclass.h>
#include <sys/malloc.h>
@@ -832,6 +833,15 @@ sys_setgroups(struct thread *td, struct setgroups_args *uap)
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 @@ sys___setugid(struct thread *td, struct __setugid_args *uap)
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);
}
/*