# HG changeset patch # User Bruno Haible # Date 1260364388 -3600 # Node ID bff2e832acadb52614b423f289d12b033ece0395 # Parent 481fcf53f002ce5c90c618005d8d19cc941f187d mgetgroups: Avoid undefined behaviour when ng == 0. diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2009-12-09 Bruno Haible + + * lib/mgetgroups.c (mgetgroups): Don't remove duplicates if there is at + most one group. + 2009-12-09 Simon Josefsson Bruno Haible diff --git a/lib/mgetgroups.c b/lib/mgetgroups.c --- a/lib/mgetgroups.c +++ b/lib/mgetgroups.c @@ -175,19 +175,20 @@ 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; + if (1 < ng) + { + gid_t first = *g; + gid_t *next; + gid_t *groups_end = g + ng; - for (next = g + 1; next < sentinel; next++) - { - if (*next == first || *next == *g) - ng--; - else - *++g = *next; - } - } + for (next = g + 1; next < groups_end; next++) + { + if (*next == first || *next == *g) + ng--; + else + *++g = *next; + } + } return ng; }