changeset 7611:07a2b2d3dc76

* lib/idcache.c: Include <stddef.h>, for offsetof. (struct userid.name): Change from char * to a flexible array member. All uses changed. * modules/idcache (Depends-on): Add flexmember.
author Paul Eggert <eggert@cs.ucla.edu>
date Mon, 06 Nov 2006 22:02:53 +0000
parents e856370de785
children 414c02fd4a6a
files ChangeLog lib/idcache.c modules/idcache
diffstat 3 files changed, 20 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2006-11-06  Paul Eggert  <eggert@cs.ucla.edu>
 
+	* lib/idcache.c: Include <stddef.h>, for offsetof.
+	(struct userid.name): Change from char * to a flexible array member.
+	All uses changed.
+	* modules/idcache (Depends-on): Add flexmember.
+
 	* MODULES.html.sh (Core language properties): New module flexmember.
 	* modules/flexmember, m4/flexmember.m4: New files.
 
--- a/lib/idcache.c
+++ b/lib/idcache.c
@@ -19,6 +19,7 @@
 
 #include <config.h>
 
+#include <stddef.h>
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
@@ -40,8 +41,8 @@
       uid_t u;
       gid_t g;
     } id;
-  char *name;
   struct userid *next;
+  char name[FLEXIBLE_ARRAY_MEMBER];
 };
 
 static struct userid *user_alist;
@@ -56,15 +57,17 @@
 {
   register struct userid *tail;
   struct passwd *pwent;
+  char const *name;
 
   for (tail = user_alist; tail; tail = tail->next)
     if (tail->id.u == uid)
       return tail->name;
 
   pwent = getpwuid (uid);
-  tail = xmalloc (sizeof *tail);
+  name = pwent ? pwent->pw_name : "";
+  tail = xmalloc (offsetof (struct userid, name) + strlen (name) + 1);
   tail->id.u = uid;
-  tail->name = pwent ? xstrdup (pwent->pw_name) : NULL;
+  strcpy (tail->name, name);
 
   /* Add to the head of the list, so most recently used is first.  */
   tail->next = user_alist;
@@ -104,8 +107,8 @@
     }
 #endif
 
-  tail = xmalloc (sizeof *tail);
-  tail->name = xstrdup (user);
+  tail = xmalloc (offsetof (struct userid, name) + strlen (user) + 1);
+  strcpy (tail->name, user);
 
   /* Add to the head of the list, so most recently used is first.  */
   if (pwent)
@@ -132,15 +135,17 @@
 {
   register struct userid *tail;
   struct group *grent;
+  char const *name;
 
   for (tail = group_alist; tail; tail = tail->next)
     if (tail->id.g == gid)
       return tail->name;
 
   grent = getgrgid (gid);
-  tail = xmalloc (sizeof *tail);
+  name = grent ? grent->gr_name : NULL;
+  tail = xmalloc (offsetof (struct userid, name) + strlen (name) + 1);
   tail->id.g = gid;
-  tail->name = grent ? xstrdup (grent->gr_name) : NULL;
+  strcpy (tail->name, name);
 
   /* Add to the head of the list, so most recently used is first.  */
   tail->next = group_alist;
@@ -180,8 +185,8 @@
     }
 #endif
 
-  tail = xmalloc (sizeof *tail);
-  tail->name = xstrdup (group);
+  tail = xmalloc (offsetof (struct userid, name) + strlen (group) + 1);
+  strcpy (tail->name, group);
 
   /* Add to the head of the list, so most recently used is first.  */
   if (grent)
--- a/modules/idcache
+++ b/modules/idcache
@@ -6,6 +6,7 @@
 m4/idcache.m4
 
 Depends-on:
+flexmember
 xalloc
 
 configure.ac: