This is the mail archive of the
cygwin-patches@cygwin.com
mailing list for the Cygwin project.
[Patch] fhandler_disk_file::opendir memory leak
- From: Ian Ray <ran_iay at yahoo dot com>
- To: cygwin-patches at cygwin dot com
- Date: Thu, 6 Nov 2003 13:15:07 +0000 (GMT)
- Subject: [Patch] fhandler_disk_file::opendir memory leak
The attached patch fixes possible memory leak in
fhandler_disk_file::opendir.
Blue skies,
Ian
________________________________________________________________________
Want to chat instantly with your online friends? Get the FREE Yahoo!
Messenger http://mail.messenger.yahoo.co.uk
2003-11-06 Ian Ray <ran_iay@yahoo.com>
* fhandler_disk_file.cc (fhandler_disk_file::opendir): Guard against
memory leak.
--- fhandler_disk_file.1.67 2003-11-06 14:48:48.959065000 +0200
+++ fhandler_disk_file.cc 2003-11-06 14:48:18.543510000 +0200
@@ -605,7 +605,7 @@ fhandler_disk_file::lock (int cmd, struc
DIR *
fhandler_disk_file::opendir ()
{
- DIR *dir;
+ DIR *dir = NULL;
DIR *res = NULL;
size_t len;
@@ -613,26 +613,14 @@ fhandler_disk_file::opendir ()
set_errno (ENOTDIR);
else if ((len = strlen (pc))> MAX_PATH - 3)
set_errno (ENAMETOOLONG);
- else if ((dir = (DIR *) malloc (sizeof (DIR))) == NULL)
+ else if ((dir = (DIR *) calloc (sizeof (DIR))) == NULL)
set_errno (ENOMEM);
else if ((dir->__d_dirname = (char *) malloc (len + 3)) == NULL)
- {
- set_errno (ENOMEM);
- free (dir);
- }
+ set_errno (ENOMEM);
else if ((dir->__d_dirent =
(struct dirent *) malloc (sizeof (struct dirent))) == NULL)
- {
- set_errno (ENOMEM);
- free (dir);
- free (dir->__d_dirname);
- }
- else if (access_worker (pc, R_OK) != 0)
- {
- free (dir);
- free (dir->__d_dirname);
- }
- else
+ set_errno (ENOMEM);
+ else if (access_worker (pc, R_OK) == 0)
{
strcpy (dir->__d_dirname, get_win32_name ());
dir->__d_dirent->d_version = __DIRENT_VERSION;
@@ -655,11 +643,21 @@ fhandler_disk_file::opendir ()
dir->__d_dirhash = get_namehash ();
res = dir;
+ dir = NULL;
}
if (pc.isencoded ())
set_encoded ();
}
+ if (dir != NULL)
+ {
+ if (dir->__d_dirname != NULL)
+ free (dir->__d_dirname);
+ if (dir->__d_dirent != NULL)
+ free (dir->__d_dirent);
+ free (dir);
+ }
+
syscall_printf ("%p = opendir (%s)", res, get_name ());
return res;
}