summaryrefslogtreecommitdiff
path: root/libpam
diff options
context:
space:
mode:
authorTomas Mraz <tmraz@fedoraproject.org>2019-11-04 08:47:16 +0100
committerTomas Mraz <tmraz@fedoraproject.org>2019-11-04 08:47:16 +0100
commitc6bef96651ee861baf099a36f0cb1fd4d36669ca (patch)
tree11c34a89eea97a3416f70b58ed23dc3c4384fa41 /libpam
parent8cfdbe5025224a712f2de79f08463cec209bd152 (diff)
Optimize the checkgrouplist function
There is no point in rising the allocation size by doubling when we can allocate required memory size at once in the second pass. * libpam/pam_modutil_ingroup.c (checkgrouplist): Allocate some reasonable default size in first pass and required size in the second pass.
Diffstat (limited to 'libpam')
-rw-r--r--libpam/pam_modutil_ingroup.c25
1 files changed, 14 insertions, 11 deletions
diff --git a/libpam/pam_modutil_ingroup.c b/libpam/pam_modutil_ingroup.c
index 875cf3e2..356302ee 100644
--- a/libpam/pam_modutil_ingroup.c
+++ b/libpam/pam_modutil_ingroup.c
@@ -12,31 +12,34 @@
#include <grp.h>
#ifdef HAVE_GETGROUPLIST
+
+#define NGROUPS_MIN 100
+#define NGROUPS_MAX 65536
+
static int checkgrouplist(const char *user, gid_t primary, gid_t target)
{
- gid_t *grouplist = NULL;
- int agroups, ngroups, i;
- ngroups = agroups = 3;
+ int ngroups, pgroups, i;
+
+ ngroups = NGROUPS_MIN;
do {
- grouplist = malloc(sizeof(gid_t) * agroups);
+ gid_t *grouplist;
+
+ pgroups = ngroups;
+ grouplist = malloc(sizeof(gid_t) * ngroups);
if (grouplist == NULL) {
return 0;
}
- ngroups = agroups;
i = getgrouplist(user, primary, grouplist, &ngroups);
- if ((i < 0) || (ngroups < 1)) {
- agroups *= 2;
- free(grouplist);
- } else {
+ if (i >= 0) {
for (i = 0; i < ngroups; i++) {
if (grouplist[i] == target) {
free(grouplist);
return 1;
}
}
- free(grouplist);
}
- } while (((i < 0) || (ngroups < 1)) && (agroups < 10000));
+ free(grouplist);
+ } while (i < 0 && ngroups > 0 && ngroups != pgroups && ngroups <= NGROUPS_MAX);
return 0;
}
#endif