Jim Meyering wrote:
> 
> Your change looks safe, so you might as well do that.
> But if you do make the change, please add an explanatory comment.

updated patch attached.

cheers,
Pádraig.
>From 93a4f892ffe2ce005a32a1b92640f061f36c0d0c Mon Sep 17 00:00:00 2001
From: =?utf-8?q?P=C3=A1draig=20Brady?= <[email protected]>
Date: Fri, 17 Apr 2009 08:25:48 +0100
Subject: [PATCH] mgetgroups: make the use of realloc more robust

* gl/lib/mgetgroups.c (mgetgroups): Handle the unlikely
case of max_n_groups==0 which would have resulted in a
double free and an erroneous error message.
* gl/modules/mgetgroups: Depend on the realloc module
which enforces some requirements of the system realloc().
---
 gl/lib/mgetgroups.c   |    7 ++++++-
 gl/modules/mgetgroups |    1 +
 2 files changed, 7 insertions(+), 1 deletions(-)

diff --git a/gl/lib/mgetgroups.c b/gl/lib/mgetgroups.c
index 736dd87..97ff466 100644
--- a/gl/lib/mgetgroups.c
+++ b/gl/lib/mgetgroups.c
@@ -91,7 +91,12 @@ mgetgroups (char const *username, gid_t gid, GETGROUPS_T **groups)
 	  if (ng < 0 && last_n_groups == max_n_groups)
 	    max_n_groups *= 2;
 
-	  if ((h = realloc_groupbuf (g, max_n_groups)) == NULL)
+	  /* Note max_n_groups may always be > 0 since the current GID
+	     is included.  However we'll check max_n_groups!=0 in the
+	     error check below both to remove this dependence and
+	     to show the correct way to handle realloc errors.  */
+	  h = realloc_groupbuf (g, max_n_groups);
+	  if (h == NULL && max_n_groups)
 	    {
 	      int saved_errno = errno;
 	      free (g);
diff --git a/gl/modules/mgetgroups b/gl/modules/mgetgroups
index 8bce53a..53c8a8d 100644
--- a/gl/modules/mgetgroups
+++ b/gl/modules/mgetgroups
@@ -9,6 +9,7 @@ m4/mgetgroups.m4
 Depends-on:
 getugroups
 xalloc
+realloc
 
 configure.ac:
 gl_MGETGROUPS
-- 
1.5.3.6

_______________________________________________
Bug-coreutils mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/bug-coreutils

Reply via email to