Skip to content

Instantly share code, notes, and snippets.

@rday
Last active July 11, 2016 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rday/e365b7d1e932b92125c22ac31c5629f2 to your computer and use it in GitHub Desktop.
Save rday/e365b7d1e932b92125c22ac31c5629f2 to your computer and use it in GitHub Desktop.
Don't sort gids that are already sorted
--- a/sys/kern/kern_prot.c
+++ b/sys/kern/kern_prot.c
@@ -2045,12 +2045,22 @@ crsetgroups_locked(struct ucred *cr, int ngrp, gid_t *groups)
int i;
int j;
gid_t g;
+ unsigned int sorted = 0;
KASSERT(cr->cr_agroups >= ngrp, ("cr_ngroups is too small"));
bcopy(groups, cr->cr_groups, ngrp * sizeof(gid_t));
cr->cr_ngroups = ngrp;
+ /* If the groups are already sorted, don't sort again */
+ for (i = 0; i < ngrp-1; i++) {
+ sorted += (cr->cr_groups[i] > cr->cr_groups[i+1]);
+ }
+
+ if (sorted==0) {
+ return;
+ }
+
/*
* Sort all groups except cr_groups[0] to allow groupmember to
* perform a binary search.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment