Subject: Re: [PATCH] abspath.c: use PATH_MAX in real_path_internal()

From: Junio C Hamano <[email-masked]>
Date: Thu, 17 Jul 2014 11:03:16 -0700

Nguyễn Thái Ngọc Duy  <[email-masked]> writes:

> This array 'cwd' is used to store the result from getcwd() and chdir()
> back. PATH_MAX is the right constant for the job. On systems with
> longer PATH_MAX (eg. 4096 on Linux), hard coding 1024 fails stuff,
> e.g. "git init". Make it static too to reduce stack usage.
>
> Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
> ---

Thanks.  It seems that this 1024 has been with us since the
beginning of this piece of code.  I briefly wondered if there are
strange platform that will have PATH_MAX shorter than 1024 that will
be hurt by this change, but the result in cwd[] is used to grow the
final result bufs[] that is sized based on PATH_MAX anyway, so it
will not be an issue (besides, the absurdly short one seems to be
a different macro, MAX_PATH, on Windows).

Will queue.

>  abspath.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/abspath.c b/abspath.c
> index ca33558..c0c868f 100644
> --- a/abspath.c
> +++ b/abspath.c
> @@ -41,7 +41,7 @@ static const char *real_path_internal(const char *path, int die_on_error)
>  	 * here so that we can chdir() back to it at the end of the
>  	 * function:
>  	 */
> -	char cwd[1024] = "";
> +	static char cwd[PATH_MAX];
>  
>  	int buf_index = 1;
>  
> @@ -49,6 +49,8 @@ static const char *real_path_internal(const char *path, int die_on_error)
>  	char *last_elem = NULL;
>  	struct stat st;
>  
> +	*cwd = '\0';
> +
>  	/* We've already done it */
>  	if (path == buf || path == next_buf)
>  		return path;