changeset 12405:fc655d62b4b9

fchdir: avoid memory leak on re-registration. Some code paths (such as dup3) could overwrite one registered directory fd with another, and must not leak the old name. * lib/fchdir.c (ensure_dirs_slot): Avoid memory leak. Signed-off-by: Eric Blake <ebb9@byu.net>
author Eric Blake <ebb9@byu.net>
date Tue, 08 Dec 2009 10:23:27 -0700
parents 44954971957b
children 9cd471f81705
files ChangeLog lib/fchdir.c
diffstat 2 files changed, 11 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2009-12-08  Eric Blake  <ebb9@byu.net>
+
+	fchdir: avoid memory leak on re-registration.
+	* lib/fchdir.c (ensure_dirs_slot): Avoid memory leak.
+
 2009-12-08  Jim Meyering  <meyering@redhat.com>
 
 	init.sh: avoid Solaris 10 /bin/sh portability problem
--- a/lib/fchdir.c
+++ b/lib/fchdir.c
@@ -60,12 +60,15 @@
 static dir_info_t *dirs;
 static size_t dirs_allocated;
 
-/* Try to ensure dirs has enough room for a slot at index fd.  Return
-   false and set errno to ENOMEM on allocation failure.  */
+/* Try to ensure dirs has enough room for a slot at index fd; free any
+   contents already in that slot.  Return false and set errno to
+   ENOMEM on allocation failure.  */
 static bool
 ensure_dirs_slot (size_t fd)
 {
-  if (fd >= dirs_allocated)
+  if (fd < dirs_allocated)
+    free (dirs[fd].name);
+  else
     {
       size_t new_allocated;
       dir_info_t *new_dirs;