We can calculate an upper bound of the number of added groups by
counting the number of delimiters in the string (plus one for the
element after the last delimiter). This avoids reallocating +1 in a
loop.
Reviewed-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
#include "alloc/reallocf.h"
#include "search/l/lsearch.h"
#include "shadowlog.h"
-
-#ident "$Id$"
+#include "string/strchr/strchrscnt.h"
/*
if (n0 == -1)
goto free_gids;
+ grouplist = REALLOCF(grouplist, n0 + strchrscnt(list, ",:") + 1, GETGROUPS_T);
+ if (grouplist == NULL)
+ return -1;
+
n = n0;
p = buf;
while (NULL != (g = strsep(&p, ",:"))) {
continue;
}
- grouplist = REALLOCF(grouplist, n + 1, GETGROUPS_T);
- if (grouplist == NULL)
- return -1;
-
LSEARCH(&grp->gr_gid, grouplist, &n);
}