# [PATCH 2/2] Handle invalid argc gently

7 messages from 2006-09-14 to 2006-09-14. Participants: Dmitry V. Levin, Junio C Hamano.
Thread: https://gitlist.dev/t/5554

## Dmitry V. Levin, 2006-09-14 01:04

Subject: [PATCH 2/2] Handle invalid argc gently
Message-ID: <20060914010409.GB20593@nomad.office.altlinux.org>
URL: https://gitlist.dev/e/20060914010409.GB20593%40nomad.office.altlinux.org

```
describe, git: Handle argc==0 case the same way as argc==1.
merge-tree: Refuse excessive arguments.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
---
 describe.c   |    2 +-
 git.c        |    2 +-
 merge-tree.c |    2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/describe.c b/describe.c
index 5ed052d..2172e8f 100644
--- a/describe.c
+++ b/describe.c
@@ -163,7 +163,7 @@ int main(int argc, char **argv)
 
 	setup_git_directory();
 
-	if (i == argc)
+	if (i >= argc)
 		describe("HEAD", 1);
 	else
 		while (i < argc) {
diff --git a/git.c b/git.c
index 47c85e1..8c182a5 100644
--- a/git.c
+++ b/git.c
@@ -294,7 +294,7 @@ static void handle_internal_command(int 
 
 int main(int argc, const char **argv, char **envp)
 {
-	const char *cmd = argv[0];
+	const char *cmd = argv[0] ? argv[0] : "git-help";
 	char *slash = strrchr(cmd, '/');
 	const char *exec_path = NULL;
 	int done_alias = 0;
diff --git a/merge-tree.c b/merge-tree.c
index c154dcf..692ede0 100644
--- a/merge-tree.c
+++ b/merge-tree.c
@@ -337,7 +337,7 @@ int main(int argc, char **argv)
 	struct tree_desc t[3];
 	void *buf1, *buf2, *buf3;
 
-	if (argc < 4)
+	if (argc != 4)
 		usage(merge_tree_usage);
 
 	setup_git_directory();


-- 
ldv

```

## Junio C Hamano, 2006-09-14 01:12

Subject: Re: [PATCH 2/2] Handle invalid argc gently
Message-ID: <7vu03b5k0t.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vu03b5k0t.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <20060914010409.GB20593@nomad.office.altlinux.org>

```
"Dmitry V. Levin" <ldv@altlinux.org> writes:

> describe, git: Handle argc==0 case the same way as argc==1.

When does one get (ac == 0)?

```

## Dmitry V. Levin, 2006-09-14 01:31

Subject: Re: [PATCH 2/2] Handle invalid argc gently
Message-ID: <20060914013146.GD7044@basalt.office.altlinux.org>
URL: https://gitlist.dev/e/20060914013146.GD7044%40basalt.office.altlinux.org
In-Reply-To: <7vu03b5k0t.fsf@assigned-by-dhcp.cox.net>

```
On Wed, Sep 13, 2006 at 06:12:02PM -0700, Junio C Hamano wrote:
> "Dmitry V. Levin" <ldv@altlinux.org> writes:
> 
> > describe, git: Handle argc==0 case the same way as argc==1.
> 
> When does one get (ac == 0)?

When one executes something like
execlp("git", NULL, NULL);


-- 
ldv

```

## Junio C Hamano, 2006-09-14 06:34

Subject: Re: [PATCH 2/2] Handle invalid argc gently
Message-ID: <7vpsdz5542.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vpsdz5542.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <20060914013146.GD7044@basalt.office.altlinux.org>

```
"Dmitry V. Levin" <ldv@altlinux.org> writes:

> On Wed, Sep 13, 2006 at 06:12:02PM -0700, Junio C Hamano wrote:
>> "Dmitry V. Levin" <ldv@altlinux.org> writes:
>> 
>> > describe, git: Handle argc==0 case the same way as argc==1.
>> 
>> When does one get (ac == 0)?
>
> When one executes something like
> execlp("git", NULL, NULL);

Fair enough.

What is the valid reason to do execlp("git", NULL, NULL)?

```

## Dmitry V. Levin, 2006-09-14 13:16

Subject: Re: [PATCH 2/2] Handle invalid argc gently
Message-ID: <20060914131626.GE6709@basalt.office.altlinux.org>
URL: https://gitlist.dev/e/20060914131626.GE6709%40basalt.office.altlinux.org
In-Reply-To: <7vpsdz5542.fsf@assigned-by-dhcp.cox.net>

```
On Wed, Sep 13, 2006 at 11:34:05PM -0700, Junio C Hamano wrote:
> "Dmitry V. Levin" <ldv@altlinux.org> writes:
> > On Wed, Sep 13, 2006 at 06:12:02PM -0700, Junio C Hamano wrote:
> >> "Dmitry V. Levin" <ldv@altlinux.org> writes:
> >> 
> >> > describe, git: Handle argc==0 case the same way as argc==1.
> >> 
> >> When does one get (ac == 0)?
> >
> > When one executes something like
> > execlp("git", NULL, NULL);
> 
> Fair enough.
> 
> What is the valid reason to do execlp("git", NULL, NULL)?

Personally I do not plan to execute git this way on regular basis, indeed. :)

But argc == 0 is allowed, so why should git crash?


-- 
ldv

```

## Junio C Hamano, 2006-09-14 16:53

Subject: Re: [PATCH 2/2] Handle invalid argc gently
Message-ID: <7virjq4cfj.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7virjq4cfj.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <20060914131626.GE6709@basalt.office.altlinux.org>

```
"Dmitry V. Levin" <ldv@altlinux.org> writes:

>> What is the valid reason to do execlp("git", NULL, NULL)?
>
> Personally I do not plan to execute git this way on regular basis, indeed. :)
>
> But argc == 0 is allowed, so why should git crash?

Oh, no I was not arguing for making git crash.  I was just
trying to learn if there is a valid reason to choose to, or
common misconfiguration that causes it to, run with ac == 0,
since I did not think of any.

It is good to be extra careful and defensive -- I just wanted to
know if there is more to it.

```

## Dmitry V. Levin, 2006-09-14 18:25

Subject: Re: [PATCH 2/2] Handle invalid argc gently
Message-ID: <20060914182539.GA12109@basalt.office.altlinux.org>
URL: https://gitlist.dev/e/20060914182539.GA12109%40basalt.office.altlinux.org
In-Reply-To: <7virjq4cfj.fsf@assigned-by-dhcp.cox.net>

```
On Thu, Sep 14, 2006 at 09:53:36AM -0700, Junio C Hamano wrote:
[...]
> >> What is the valid reason to do execlp("git", NULL, NULL)?
> >
> > Personally I do not plan to execute git this way on regular basis, indeed. :)
> >
> > But argc == 0 is allowed, so why should git crash?
> 
> Oh, no I was not arguing for making git crash.  I was just
> trying to learn if there is a valid reason to choose to, or
> common misconfiguration that causes it to, run with ac == 0,
> since I did not think of any.

Well, the execlp example is somewhat dragged in.
More common case is execvp:

char *argv[] = { NULL };
execvp("git", argv);

The argv array may be empty for various reasons, e.g. pointer arithmetic
mistakes.


-- 
ldv

```
