This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH v2] Fix resource leak in getcwd()


The fix is similar to the code in the current FreeBSD sources which
fixed this bug in 1997.

newlib/ChangeLog
2014-10-16  Sebastian Huber  <sebastian.huber@embedded-brains.de>

	* libc/unix/getcwd.c (getcwd): Close directory also in case of
	an error.
---
 newlib/libc/unix/getcwd.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/newlib/libc/unix/getcwd.c b/newlib/libc/unix/getcwd.c
index 92f1b20..63f3424 100644
--- a/newlib/libc/unix/getcwd.c
+++ b/newlib/libc/unix/getcwd.c
@@ -57,7 +57,7 @@ getcwd (pt, size)
      size_t size;
 {
   register struct dirent *dp;
-  register DIR *dir;
+  register DIR *dir = NULL;
   register dev_t dev;
   register ino_t ino;
   register int first;
@@ -237,6 +237,7 @@ getcwd (pt, size)
       bpt -= strlen (dp->d_name);
       bcopy (dp->d_name, bpt, strlen (dp->d_name));
       (void) closedir (dir);
+      dir = NULL;
 
       /* Truncate any file name. */
       *bup = '\0';
@@ -256,6 +257,8 @@ notfound:
 err:
   if (ptsize)
     free (pt);
+  if (dir)
+    (void) closedir (dir);
   free (up);
   return (char *) NULL;
 }
-- 
1.8.4.5


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]