Subject: Re: [PATCH] abspath: increase array size of cwd variable to PATH_MAX

From: wanghui <[email-masked]>
Date: Thu, 22 Sep 2011 16:54:16 +0800

Junio C Hamano wrote:
> Ramsay Jones <[email-masked]> writes:
>
>   
>> Hmm, the subject line says "... increase array size ...", but that is not
>> necessarily what this patch is doing! :-D
>>     
>
> True; will revert it out of 'next'.
>
> Thanks for noticing.
>
>   
Hi Junio and Ramsay,

In fact, i found lots of similar usage in the git source code. E.G.

in the dir.c,
int is_inside_dir(const char *dir)
{
char cwd[PATH_MAX];
if (!dir)
return 0;
if (!getcwd(cwd, sizeof(cwd)))
die_errno("can't find the current directory");
return dir_inside_of(cwd, dir) >= 0;
}

Since this implementation is not cross-OS safe, Could we use below solution?

step1, add a new function xgetcwd() to wrapper getcwd() like this:
char *xgetcwd(void)
{
size_t size = 100;

while (1) {
char *buffer = (char *) xmalloc (size);
if (getcwd (buffer, size) == buffer)
return buffer;
free (buffer);
if (errno != ERANGE)
return 0;
size *= 2;
}
}

step2, use xgetcwd to replace all getcwd occurrence in the git source code.

This will add a little bit overhead to the git, but it is cross-OS safe.