public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Fix resource leak in getcwd()
@ 2014-10-16 10:36 Sebastian Huber
  2014-10-16 11:19 ` Sebastian Huber
  0 siblings, 1 reply; 2+ messages in thread
From: Sebastian Huber @ 2014-10-16 10:36 UTC (permalink / raw)
  To: newlib; +Cc: Sebastian Huber

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 | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/newlib/libc/unix/getcwd.c b/newlib/libc/unix/getcwd.c
index 92f1b20..7292445 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;
@@ -163,6 +163,8 @@ getcwd (pt, size)
       *bup = '\0';
 
       /* Open and stat parent directory. */
+      if (dir)
+	(void) closedir (dir);
       if (!(dir = opendir (up)) || fstat (__dirfd (dir), &s))
 	goto err;
 
@@ -236,7 +238,6 @@ getcwd (pt, size)
 	*--bpt = '/';
       bpt -= strlen (dp->d_name);
       bcopy (dp->d_name, bpt, strlen (dp->d_name));
-      (void) closedir (dir);
 
       /* 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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Fix resource leak in getcwd()
  2014-10-16 10:36 [PATCH] Fix resource leak in getcwd() Sebastian Huber
@ 2014-10-16 11:19 ` Sebastian Huber
  0 siblings, 0 replies; 2+ messages in thread
From: Sebastian Huber @ 2014-10-16 11:19 UTC (permalink / raw)
  To: newlib

On 16/10/14 12:36, Sebastian Huber wrote:
> 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.

Sorry, this introduces another resource leak.  I will send a new patch soon.

-- 
Sebastian Huber, embedded brains GmbH

Address : Dornierstr. 4, D-82178 Puchheim, Germany
Phone   : +49 89 189 47 41-16
Fax     : +49 89 189 47 41-09
E-Mail  : sebastian.huber@embedded-brains.de
PGP     : Public key available on request.

Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-10-16 11:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-16 10:36 [PATCH] Fix resource leak in getcwd() Sebastian Huber
2014-10-16 11:19 ` Sebastian Huber

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).