From b9f131c820ef35c46d4a32e01d3ec8250a952457 Mon Sep 17 00:00:00 2001 From: Christopher Faylor Date: Mon, 2 Jan 2012 17:45:51 +0000 Subject: [PATCH] * path.cc (get_current_dir_name): Avoid memory leak. Don't return PWD contents if directory doesn't actually exist. --- winsup/cygwin/ChangeLog | 5 +++++ winsup/cygwin/path.cc | 19 +++++++++---------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog index 528e0efa2..a696795be 100644 --- a/winsup/cygwin/ChangeLog +++ b/winsup/cygwin/ChangeLog @@ -1,3 +1,8 @@ +2012-01-02 Christopher Faylor + + * 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 * cygwin.din (get_current_dir_name): Export. diff --git a/winsup/cygwin/path.cc b/winsup/cygwin/path.cc index 7dc85442e..bbc93afbc 100644 --- a/winsup/cygwin/path.cc +++ b/winsup/cygwin/path.cc @@ -2858,19 +2858,18 @@ 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); + struct __stat64 pwdbuf, cwdbuf; - if (pwd) + 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) { - struct __stat64 pwdbuf, cwdbuf; - stat64 (pwd, &pwdbuf); - stat64 (cwd, &cwdbuf); - if ((pwdbuf.st_dev == cwdbuf.st_dev) && (pwdbuf.st_ino == cwdbuf.st_ino)) - { - cwd = (char *) malloc (strlen (pwd) + 1); - strcpy (cwd, pwd); - } + cwd = (char *) realloc (cwd, strlen (pwd) + 1); + strcpy (cwd, pwd); } return cwd;