From d49061eb73ed801cda8a52b65c18b1ee12189d66 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Wed, 9 Dec 2009 14:13:08 +0100 Subject: [PATCH] mgetgroups: Avoid undefined behaviour when ng == 0. --- ChangeLog | 5 +++++ lib/mgetgroups.c | 27 ++++++++++++++------------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index ecc461f05..4009854da 100644 --- 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 index 5ca450f9d..89d161846 100644 --- a/lib/mgetgroups.c +++ b/lib/mgetgroups.c @@ -175,19 +175,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; } -- 2.11.0