X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fmgetgroups.c;h=76474c21b9936e6138e401026b67e4c2582f3701;hb=ab43907148e80f3895280928c05d4be93875580f;hp=5ca450f9da9fd3bc5c99e2a9ee7134cc2bdad483;hpb=b1ab442e6db76de2a7315531f6f0ec59c5934368;p=gnulib.git diff --git a/lib/mgetgroups.c b/lib/mgetgroups.c index 5ca450f9d..76474c21b 100644 --- a/lib/mgetgroups.c +++ b/lib/mgetgroups.c @@ -1,6 +1,6 @@ /* mgetgroups.c -- return a list of the groups a user or current process is in - Copyright (C) 2007-2009 Free Software Foundation, Inc. + Copyright (C) 2007-2010 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -141,7 +141,8 @@ mgetgroups (char const *username, gid_t gid, gid_t **groups) ng = (username ? getugroups (max_n_groups, g, username, gid) - : getgroups (max_n_groups, g + (gid != (gid_t) -1))); + : getgroups (max_n_groups - (gid != (gid_t) -1), + g + (gid != (gid_t) -1))); if (ng < 0) { @@ -175,19 +176,20 @@ mgetgroups (char const *username, gid_t gid, gid_t **groups) duplicate removal via an O(n) hash-table. Hence, this function is only documented as guaranteeing no pair-wise duplicates, rather than returning the minimal set. */ - { - gid_t first = *g; - gid_t *next; - gid_t *sentinel = g + ng; - - for (next = g + 1; next < sentinel; next++) - { - if (*next == first || *next == *g) - ng--; - else - *++g = *next; - } - } + if (1 < ng) + { + gid_t first = *g; + gid_t *next; + gid_t *groups_end = g + ng; + + for (next = g + 1; next < groups_end; next++) + { + if (*next == first || *next == *g) + ng--; + else + *++g = *next; + } + } return ng; }