rev-parse: check i before using argv[i] against argc
The --prefix, --default, and --resolve-git-dir options to git-rev-parse require an argument, but when given no argument, the code uses the NULL read from argv[argc] without checking, leading to a segfault. Instead, check first and die() with an error message. Signed-off-by: David Sharp <dhsharp@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
d2446dfd7f
commit
a43219f2aa
@ -523,15 +523,17 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!strcmp(arg, "--default")) {
|
if (!strcmp(arg, "--default")) {
|
||||||
def = argv[i+1];
|
def = argv[++i];
|
||||||
i++;
|
if (!def)
|
||||||
|
die("--default requires an argument");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!strcmp(arg, "--prefix")) {
|
if (!strcmp(arg, "--prefix")) {
|
||||||
prefix = argv[i+1];
|
prefix = argv[++i];
|
||||||
|
if (!prefix)
|
||||||
|
die("--prefix requires an argument");
|
||||||
startup_info->prefix = prefix;
|
startup_info->prefix = prefix;
|
||||||
output_prefix = 1;
|
output_prefix = 1;
|
||||||
i++;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!strcmp(arg, "--revs-only")) {
|
if (!strcmp(arg, "--revs-only")) {
|
||||||
@ -703,9 +705,12 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!strcmp(arg, "--resolve-git-dir")) {
|
if (!strcmp(arg, "--resolve-git-dir")) {
|
||||||
const char *gitdir = resolve_gitdir(argv[i+1]);
|
const char *gitdir = argv[++i];
|
||||||
if (!gitdir)
|
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);
|
puts(gitdir);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user