# [PATCH] Keep "git --git-dir" from causing a bus error.

3 messages from 2006-12-22 to 2006-12-22. Participants: Brian Gernhardt, Johannes Schindelin.
Thread: https://gitlist.dev/t/6079

## Brian Gernhardt, 2006-12-22 13:56

Subject: [PATCH] Keep "git --git-dir" from causing a bus error.
Message-ID: <20061222135625.GA26084@179.242.249.10.in-addr.arpa>
URL: https://gitlist.dev/e/20061222135625.GA26084%40179.242.249.10.in-addr.arpa

```
The option checking code for --git-dir had an off by 1 error that
would cause it to access uninitialized memory if it was the last
argument.  This causes it to display an error and display the usage
string instead.

Signed-off-by: Brian Gernhardt <benji@silverinsanity.com>
---

 I would have made this display the git directory, but the code to
 do that seems to be unique to rev-parse and involve more set up than is
 done at the time the option is parsed.

 git.c |   13 ++++++++-----
 1 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/git.c b/git.c
index 73cf4d4..2b3c9f9 100644
--- a/git.c
+++ b/git.c
@@ -59,11 +59,14 @@ static int handle_options(const char*** argv, int* argc)
 		} else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
 			setup_pager();
 		} else if (!strcmp(cmd, "--git-dir")) {
-			if (*argc < 1)
-				return -1;
-			setenv("GIT_DIR", (*argv)[1], 1);
-			(*argv)++;
-			(*argc)--;
+			if (*argc < 2) {
+				fprintf(stderr, "No directory given for --git-dir.\n" );
+				usage(git_usage_string);
+			} else {
+				setenv("GIT_DIR", (*argv)[1], 1);
+				(*argv)++;
+				(*argc)--;
+			}
 		} else if (!strncmp(cmd, "--git-dir=", 10)) {
 			setenv("GIT_DIR", cmd + 10, 1);
 		} else if (!strcmp(cmd, "--bare")) {
-- 
1.4.4.GIT

```

## Johannes Schindelin, 2006-12-22 16:08

Subject: Re: [PATCH] Keep "git --git-dir" from causing a bus error.
Message-ID: <Pine.LNX.4.63.0612221708070.19693@wbgn013.biozentrum.uni-wuerzburg.de>
URL: https://gitlist.dev/e/Pine.LNX.4.63.0612221708070.19693%40wbgn013.biozentrum.uni-wuerzburg.de
In-Reply-To: <20061222135625.GA26084@179.242.249.10.in-addr.arpa>

```
Hi,

On Fri, 22 Dec 2006, Brian Gernhardt wrote:

> diff --git a/git.c b/git.c
> index 73cf4d4..2b3c9f9 100644
> --- a/git.c
> +++ b/git.c
> @@ -59,11 +59,14 @@ static int handle_options(const char*** argv, int* argc)
>  		} else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
>  			setup_pager();
>  		} else if (!strcmp(cmd, "--git-dir")) {
> -			if (*argc < 1)
> -				return -1;
> -			setenv("GIT_DIR", (*argv)[1], 1);
> -			(*argv)++;
> -			(*argc)--;
> +			if (*argc < 2) {
> +				fprintf(stderr, "No directory given for --git-dir.\n" );
> +				usage(git_usage_string);

Usage already exits, so you do not need this:

> +			} else {

Ciao,
Dscho

```

## Brian Gernhardt, 2006-12-22 17:34

Subject: Re: [PATCH] Keep "git --git-dir" from causing a bus error.
Message-ID: <2E577524-FAFC-4A9C-809D-B3698682FDEB@silverinsanity.com>
URL: https://gitlist.dev/e/2E577524-FAFC-4A9C-809D-B3698682FDEB%40silverinsanity.com
In-Reply-To: <Pine.LNX.4.63.0612221708070.19693@wbgn013.biozentrum.uni-wuerzburg.de>

```

On Dec 22, 2006, at 11:08 AM, Johannes Schindelin wrote:

>> +				fprintf(stderr, "No directory given for --git-dir.\n" );
>> +				usage(git_usage_string);
>
> Usage already exits, so you do not need this:
>
>> +			} else {

I suppose not.  It came out of a) habit and b) experiments with  
things that did not exit.

~~ Brian

```
