This is the mail archive of the cygwin-patches mailing list for the Cygwin 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]

Re: [PATCH] Add get_current_dir_name(3)


On Mon, Jan 02, 2012 at 05:56:26AM -0700, Eric Blake wrote:
>On 01/01/2012 12:13 AM, Yaakov (Cygwin/X) wrote:
>>> > You have to check st_dev here too don't you?
>> Of course.  Revised patch for winsup/cygwin attached.
>> 
>
>> +extern "C" char *
>> +get_current_dir_name (void)
>> +{
>> +  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))
>> +        {
>> +          cwd = (char *) malloc (strlen (pwd) + 1);
>
>Memory leak.  You need to free(cwd) before reassigning it.  And why are
>you using malloc(strlen())/strcpy(), when you could just use strdup()?

Oops.  Eric is right.  Also, there should have been some error checking
for the stat calls since PWD is user-settable and could be bogus.  I'll
make those changes.

cgf


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