"Jordan Crouse" <jordan.crouse@amd.com> says:
authorRob Landley <rob@landley.net>
Tue, 22 Aug 2006 23:40:28 +0000 (23:40 -0000)
committerRob Landley <rob@landley.net>
Tue, 22 Aug 2006 23:40:28 +0000 (23:40 -0000)
The following patch makes coreutils/test.c act fail gracefully if getgroups()
returns a -1.  This fixes a problem on the One Laptop Per Child ROM image
whereby we were getting odd Memory exhausted messages for '[' and 'test'.

Found by Mitch Bradley <wmb@firmworks.com>
(Tweaked by Rob: no need to initialize a static to NULL, or realloc something
that's only allocated when it's NULL.)

coreutils/test.c

index bbc80228339cd3cc0c9d9a9d6fd7c93575afa42a..c1097c28c5e8e7c873cd26846e4550b2feecbc74 100644 (file)
@@ -151,7 +151,7 @@ typedef int arith_t;
 
 static char **t_wp;
 static struct t_op const *t_wp_op;
-static gid_t *group_array = NULL;
+static gid_t *group_array;
 static int ngroups;
 
 static enum token t_lex(char *s);
@@ -547,8 +547,10 @@ static int test_eaccess(char *path, int mode)
 static void initialize_group_array(void)
 {
        ngroups = getgroups(0, NULL);
-       group_array = xrealloc(group_array, ngroups * sizeof(gid_t));
-       getgroups(ngroups, group_array);
+       if (ngroups > 0) {
+               group_array = xmalloc(ngroups * sizeof(gid_t));
+               getgroups(ngroups, group_array);
+       }
 }
 
 /* Return non-zero if GID is one that we have in our groups list. */