Skip to contentSkip to conversation

Handle invalid argc gently

PATCH NAK Dmitry V. Levin 2 participants 7 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 Handle invalid argc gently

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

---
 describe.c   |    2 +-
 git.c        |    2 +-
 merge-tree.c |    2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
describe.c +1 -1
index 5ed052d..2172e8f 100644
@@ -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) {
git.c +1 -1
index 47c85e1..8c182a5 100644
@@ -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;
merge-tree.c +1 -1
index c154dcf..692ede0 100644
@@ -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();
Signature · 1 line
ldv
#1 lore raw
Quoted 1 line · Dmitry V. Levin

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

When does one get (ac == 0)?

#2 lore raw
Quoted 5 lines · Junio C Hamano
Quoted 1 line · Dmitry V. Levin

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);

Signature · 1 line
ldv
#3 lore raw
Quoted 9 lines · Dmitry V. Levin
Quoted 5 lines · Junio C Hamano
Quoted 1 line · Dmitry V. Levin

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)?

#4 lore raw
Quoted 6 lines · Junio C Hamano
Quoted 5 lines · Dmitry V. Levin
Quoted 4 lines · Junio C Hamano
Quoted 1 line · Dmitry V. Levin

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

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

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?

Signature · 1 line
ldv
#5 lore raw
Quoted 5 lines · Dmitry V. Levin
Quoted 1 line

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.

#6 lore raw

On Thu, Sep 14, 2006 at 09:53:36AM -0700, Junio C Hamano wrote:
[...]

Quoted 10 lines
Quoted 5 lines
Quoted 1 line

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.

Signature · 1 line
ldv