* path.cc (get_current_dir_name): Avoid memory leak. Don't return PWD contents

if directory doesn't actually exist.
This commit is contained in:
Christopher Faylor 2012-01-02 17:45:51 +00:00
parent 7e89497328
commit b9f131c820
2 changed files with 14 additions and 10 deletions

View File

@ -1,3 +1,8 @@
2012-01-02 Christopher Faylor <me.cygwin2011@cgf.cx>
* path.cc (get_current_dir_name): Avoid memory leak. Don't return PWD
contents if directory doesn't actually exist.
2012-01-01 Yaakov Selkowitz <yselkowitz@users.sourceforge.net>
* cygwin.din (get_current_dir_name): Export.

View File

@ -2858,20 +2858,19 @@ getwd (char *buf)
extern "C" char *
get_current_dir_name (void)
{
char *pwd = getenv ("PWD");
const char *pwd = getenv ("PWD");
char *cwd = getcwd (NULL, 0);
if (pwd)
{
struct __stat64 pwdbuf, cwdbuf;
stat64 (pwd, &pwdbuf);
stat64 (cwd, &cwdbuf);
if ((pwdbuf.st_dev == cwdbuf.st_dev) && (pwdbuf.st_ino == cwdbuf.st_ino))
if (pwd && strcmp (pwd, cwd) != 0
&& stat64 (pwd, &pwdbuf) == 0
&& stat64 (cwd, &cwdbuf) == 0
&& pwdbuf.st_dev == cwdbuf.st_dev
&& pwdbuf.st_ino == cwdbuf.st_ino)
{
cwd = (char *) malloc (strlen (pwd) + 1);
cwd = (char *) realloc (cwd, strlen (pwd) + 1);
strcpy (cwd, pwd);
}
}
return cwd;
}