threads / patch / 6079

patchKeep "git --git-dir" from causing a bus error.

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

## tl;dr

3 messages between Dec 22, 2006 and Dec 22, 2006. Diffs are folded; open one to read it.

replies: 2people: 2as markdown or json

Brian Gernhardt· Dec 22, 2006, 13:56 UTC · lore

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(-)
Show changes to git.c +8 −5
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· Dec 22, 2006, 16:08 UTC · re: Brian Gernhardt · lore

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

Hi,
On Fri, 22 Dec 2006, Brian Gernhardt wrote:
Show 16 quoted lines
> 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· Dec 22, 2006, 17:34 UTC · re: Johannes Schindelin · lore

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

On Dec 22, 2006, at 11:08 AM, Johannes Schindelin wrote:
Show 6 quoted lines
>> +				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

← back to recent threads