# [PATCH] Setup.c: PATH_MAX is the length including the Nil

2 messages from 2014-03-04 to 2014-03-04. Participants: Sun He, Faiz Kothari.
Thread: https://gitlist.dev/t/36052

## Sun He, 2014-03-04 16:29

Subject: [PATCH] Setup.c: PATH_MAX is the length including the Nil
Message-ID: <1393950550-9668-1-git-send-email-sunheehnus@gmail.com>
URL: https://gitlist.dev/e/1393950550-9668-1-git-send-email-sunheehnus%40gmail.com

```
Signed-off-by: Sun He <sunheehnus@gmail.com>
---

Check the limit.h of linux and find out that the MACRO
#define PATH_MAX        4096	/* # chars in a path name including nul */
So if the magic number 40 is just the size it should be. (e.g. hash code)
It may bring bugs with the length(4056) of long name(gitdirenv).
As gitdirenv could be set by GIT_DIR_ENVIRONMENT.
If it is a bug, it will almost never occur.
But I need your help to know if there is the PATH_MAX of git is the mirror of the
PATH_MAX of linux and if this fix is right?
If it was, there may be many places like "PATH_MAX + 1" could be replaced by
just "PATH_MAX". And there may be many places like this.

Cheers,
He Sun

 setup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/setup.c b/setup.c
index cffb6d6..1511612 100644
--- a/setup.c
+++ b/setup.c
@@ -395,7 +395,7 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
 	char *gitfile;
 	int offset;
 
-	if (PATH_MAX - 40 < strlen(gitdirenv))
+	if (PATH_MAX - 41 < strlen(gitdirenv))
 		die("'$%s' too big", GIT_DIR_ENVIRONMENT);
 
 	gitfile = (char*)read_gitfile(gitdirenv);
-- 
1.9.0.138.g2de3478.dirty

```

## Faiz Kothari, 2014-03-04 16:49

Subject: Re: [PATCH] Setup.c: PATH_MAX is the length including the Nil
Message-ID: <CAFbjVc=bt4NX2uAPx98YCqEh_gKY=KVsm1iFHU8N86aqKL3VoQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAFbjVc%3Dbt4NX2uAPx98YCqEh_gKY%3DKVsm1iFHU8N86aqKL3VoQ%40mail.gmail.com
In-Reply-To: <1393950550-9668-1-git-send-email-sunheehnus@gmail.com>

```
On Tue, Mar 4, 2014 at 9:59 PM, Sun He <sunheehnus@gmail.com> wrote:
> Signed-off-by: Sun He <sunheehnus@gmail.com>
> ---
>
> Check the limit.h of linux and find out that the MACRO
> #define PATH_MAX        4096    /* # chars in a path name including nul */
> So if the magic number 40 is just the size it should be. (e.g. hash code)
> It may bring bugs with the length(4056) of long name(gitdirenv).
> As gitdirenv could be set by GIT_DIR_ENVIRONMENT.
> If it is a bug, it will almost never occur.
> But I need your help to know if there is the PATH_MAX of git is the mirror of the
> PATH_MAX of linux and if this fix is right?
> If it was, there may be many places like "PATH_MAX + 1" could be replaced by
> just "PATH_MAX". And there may be many places like this.
>
> Cheers,
> He Sun

Hi,
I am not getting what exactly you are trying to tell, but git defines
its own PATH_MAX.
Its defined in git-compat-util.h: #define PATH_MAX 4096
That is why instead of making buffers using PATH_MAX, use strbuf.
All these problems of buffer overflow will be gone.
I hope you are concerned about buffer overflow.

Cheers

-Faiz

```
