mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Collin Funk <collin.funk1@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Collin Funk <collin.funk1@gmail.com>
Subject: [PATCH] groups: Use bsearch instead of hand rolled implementation
Date: Sun, 25 May 2025 17:17:46 -0700	[thread overview]
Message-ID: <ab5708c1e35e1b2a54a1d83fafda1b3f8fa01103.1748218528.git.collin.funk1@gmail.com> (raw)

This code predates <linux/bsearch.h>. Now that the bsearch function
exists there we can use it to reduce code duplication.

Signed-off-by: Collin Funk <collin.funk1@gmail.com>
---
 kernel/groups.c | 19 +++++--------------
 1 file changed, 5 insertions(+), 14 deletions(-)

diff --git a/kernel/groups.c b/kernel/groups.c
index 9b43da22647d..4294a97e4ea8 100644
--- a/kernel/groups.c
+++ b/kernel/groups.c
@@ -2,6 +2,7 @@
 /*
  * Supplementary group IDs
  */
+#include <linux/bsearch.h>
 #include <linux/cred.h>
 #include <linux/export.h>
 #include <linux/slab.h>
@@ -91,23 +92,13 @@ EXPORT_SYMBOL(groups_sort);
 /* a simple bsearch */
 int groups_search(const struct group_info *group_info, kgid_t grp)
 {
-	unsigned int left, right;
+	void *result;
 
 	if (!group_info)
 		return 0;
-
-	left = 0;
-	right = group_info->ngroups;
-	while (left < right) {
-		unsigned int mid = (left+right)/2;
-		if (gid_gt(grp, group_info->gid[mid]))
-			left = mid + 1;
-		else if (gid_lt(grp, group_info->gid[mid]))
-			right = mid;
-		else
-			return 1;
-	}
-	return 0;
+	result = bsearch(&grp, group_info->gid, group_info->ngroups,
+			 sizeof(*group_info->gid), gid_cmp);
+	return !!result;
 }
 
 /**
-- 
2.49.0


             reply	other threads:[~2025-05-26  0:18 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-26  0:17 Collin Funk [this message]
2025-05-26  0:25 ` Al Viro
2025-05-26 17:46   ` Collin Funk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ab5708c1e35e1b2a54a1d83fafda1b3f8fa01103.1748218528.git.collin.funk1@gmail.com \
    --to=collin.funk1@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®