Re: [PATCH] rev-parse: Check argc before using argv[i+1]

5 messages, 3 authors, 2016-06-15 · open the first message on its own page

Re: [PATCH] rev-parse: Check argc before using argv[i+1]

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:59:46

David Sharp [off-list ref] writes:
quoted hunk
@@ -738,9 +740,11 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 				continue;
 			}
 			if (!strcmp(arg, "--resolve-git-dir")) {
-				const char *gitdir = resolve_gitdir(argv[i+1]);
+				if (++i >= argc)
+					die("--resolve-git-dir requires an argument");
+				const char *gitdir = resolve_gitdir(argv[i]);
 				if (!gitdir)
-					die("not a gitdir '%s'", argv[i+1]);
+					die("not a gitdir '%s'", argv[i]);
This adds decl-after-statement.  As referencing argv[argc] is not a
crime (you will grab a NULL), how about doing it this way to see if
the value is a NULL, instead of comparing the index and argc?

	const char *gitdir;
        if (!argv[++i])
        	die("--resolve-git-dir requires an argument");
	gitdir = resolve_gitdir(argv[i]);
        if (!gitdir)
        	die("not a gitdir '%s'", argv[i]);

Same comment may apply to other hunks.

Thanks.

Re: [PATCH] rev-parse: Check argc before using argv[i+1]

From: David Sharp <hidden>
Date: 2016-06-15 22:59:46

On Tue, Jan 28, 2014 at 11:12 AM, Junio C Hamano [off-list ref] wrote:
David Sharp [off-list ref] writes:
quoted
@@ -738,9 +740,11 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                              continue;
                      }
                      if (!strcmp(arg, "--resolve-git-dir")) {
-                             const char *gitdir = resolve_gitdir(argv[i+1]);
+                             if (++i >= argc)
+                                     die("--resolve-git-dir requires an argument");
+                             const char *gitdir = resolve_gitdir(argv[i]);
                              if (!gitdir)
-                                     die("not a gitdir '%s'", argv[i+1]);
+                                     die("not a gitdir '%s'", argv[i]);
This adds decl-after-statement.
Sorry, I wasn't aware that git is trying to conform to C90. (It's not
compiled with -std=c90, -ansi or -Wdeclaration-after-statement.)
As referencing argv[argc] is not a
crime (you will grab a NULL), how about doing it this way to see if
the value is a NULL, instead of comparing the index and argc?
I'm not convinced this is any better, but alright. I did something
slightly different based on that, though.

v2 patch incoming...
        const char *gitdir;
        if (!argv[++i])
                die("--resolve-git-dir requires an argument");
        gitdir = resolve_gitdir(argv[i]);
        if (!gitdir)
                die("not a gitdir '%s'", argv[i]);

Same comment may apply to other hunks.

Thanks.

[PATCH v2] rev-parse: Check argc before using argv[i+1]

From: David Sharp <hidden>
Date: 2016-06-15 22:59:46

Without this patch, git-rev-parse --prefix, --default, or
--resolve-git-dir, without a value argument, would result in a segfault.
Instead, die() with a message.

Signed-off-by: David Sharp <redacted>
---
 builtin/rev-parse.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index aaeb611..45901df 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -547,15 +547,17 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 				continue;
 			}
 			if (!strcmp(arg, "--default")) {
-				def = argv[i+1];
-				i++;
+				def = argv[++i];
+				if (!def)
+					die("--default requires an argument");
 				continue;
 			}
 			if (!strcmp(arg, "--prefix")) {
-				prefix = argv[i+1];
+				prefix = argv[++i];
+				if (!prefix)
+					die("--prefix requires an argument");
 				startup_info->prefix = prefix;
 				output_prefix = 1;
-				i++;
 				continue;
 			}
 			if (!strcmp(arg, "--revs-only")) {
@@ -738,9 +740,12 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 				continue;
 			}
 			if (!strcmp(arg, "--resolve-git-dir")) {
-				const char *gitdir = resolve_gitdir(argv[i+1]);
+				const char *gitdir = argv[++i];
 				if (!gitdir)
-					die("not a gitdir '%s'", argv[i+1]);
+					die("--resolve-git-dir requires an argument");
+				gitdir = resolve_gitdir(gitdir);
+				if (!gitdir)
+					die("not a gitdir '%s'", argv[i]);
 				puts(gitdir);
 				continue;
 			}
-- 
1.8.5.3

Re: [PATCH v2] rev-parse: Check argc before using argv[i+1]

From: Johannes Sixt <hidden>
Date: 2016-06-15 22:59:46

Am 28.01.2014 22:21, schrieb David Sharp:
quoted hunk
@@ -738,9 +740,12 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 				continue;
 			}
 			if (!strcmp(arg, "--resolve-git-dir")) {
-				const char *gitdir = resolve_gitdir(argv[i+1]);
+				const char *gitdir = argv[++i];
 				if (!gitdir)
-					die("not a gitdir '%s'", argv[i+1]);
+					die("--resolve-git-dir requires an argument");
+				gitdir = resolve_gitdir(gitdir);
+				if (!gitdir)
+					die("not a gitdir '%s'", argv[i]);
 				puts(gitdir);
 				continue;
 			}
In this hunk, I don't see where the old code incremented i for the
argument. Was this another bug lurking that is fixed now?

-- Hannes

Re: [PATCH v2] rev-parse: Check argc before using argv[i+1]

From: David Sharp <hidden>
Date: 2016-06-15 22:59:46

On Tue, Jan 28, 2014 at 2:01 PM, Johannes Sixt [off-list ref] wrote:
Am 28.01.2014 22:21, schrieb David Sharp:
quoted
@@ -738,9 +740,12 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                              continue;
                      }
                      if (!strcmp(arg, "--resolve-git-dir")) {
-                             const char *gitdir = resolve_gitdir(argv[i+1]);
+                             const char *gitdir = argv[++i];
                              if (!gitdir)
-                                     die("not a gitdir '%s'", argv[i+1]);
+                                     die("--resolve-git-dir requires an argument");
+                             gitdir = resolve_gitdir(gitdir);
+                             if (!gitdir)
+                                     die("not a gitdir '%s'", argv[i]);
                              puts(gitdir);
                              continue;
                      }
In this hunk, I don't see where the old code incremented i for the
argument. Was this another bug lurking that is fixed now?
Yup. I did notice that too. Should have mentioned it in the description.
-- Hannes
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help