test-subprocess: fix segfault without arguments
Check if we even have a parameter before checking its value. Running this command without any arguments may not make a lot of sense, but reacting with a segmentation fault is unduly harsh. While we're at it, avoid casting argv by declaring it const right away. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Apr 10, 2012 at 21:07 UTC
a961d1f1ea2e3aaef0ff3e75920d1cd830d9b423
1 file changed
+3
-3
test-subprocess.c
+3
-3
@@ -1,7 +1,7 @@
1
#include "cache.h"
2
#include "run-command.h"
3
4
-int main(int argc, char **argv)
4
+int main(int argc, const char **argv)
5
{
6
struct child_process cp;
7
int nogit = 0;
@@ -9,12 +9,12 @@ int main(int argc, char **argv)
9
setup_git_directory_gently(&nogit);
10
if (nogit)
11
die("No git repo found");
12
- if (!strcmp(argv[1], "--setup-work-tree")) {
12
+ if (argc > 1 && !strcmp(argv[1], "--setup-work-tree")) {
13
setup_work_tree();
14
argv++;
15
}
16
memset(&cp, 0, sizeof(cp));
17
cp.git_cmd = 1;
18
- cp.argv = (const char **)argv+1;
18
+ cp.argv = argv + 1;
19
return run_command(&cp);
20
}