mgetgroups: do not write bytes beyond end of malloc'd buffer
authorJim Meyering <meyering@redhat.com>
Thu, 10 Dec 2009 11:17:19 +0000 (12:17 +0100)
committerJim Meyering <meyering@redhat.com>
Thu, 10 Dec 2009 11:17:19 +0000 (12:17 +0100)
* lib/mgetgroups.c: Fix an off-by-one error.  When we have no
username, we call getgroups with a one-element-shorter buffer,
but still told it the length was original, max_n_groups.

ChangeLog
lib/mgetgroups.c

index 4b150e0dad240d3c324fcc42bbf2dd5e4dc11ea4..c656180fed018f2ea8e9a7e692c1d9ca9bde54d8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2009-12-10  Jim Meyering  <meyering@redhat.com>
+
+       mgetgroups: do not write bytes beyond end of malloc'd buffer
+       * lib/mgetgroups.c: Fix an off-by-one error.  When we have no
+       username, we call getgroups with a one-element-shorter buffer,
+       but still told it the length was original, max_n_groups.
+
 2009-12-09  Eric Blake  <ebb9@byu.net>
 
        cloexec: relax license
index 89d161846a6288398e5a022047300a3f05d2ae25..0f853d6f8b2c1a56aab1cd6140e5c79402f568ad 100644 (file)
@@ -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)
     {