apply realpath() to all paths in argv; fix some leaks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
diff --git a/got/got.c b/got/got.c
index 660a5a1..7843bcd 100644
--- a/got/got.c
+++ b/got/got.c
@@ -185,34 +185,47 @@ cmd_checkout(int argc, char *argv[])
#endif
if (argc == 1) {
char *cwd, *base, *dotgit;
- repo_path = argv[0];
+ repo_path = realpath(argv[0], NULL);
+ if (repo_path == NULL)
+ return got_error_from_errno();
cwd = getcwd(NULL, 0);
- if (cwd == NULL)
- err(1, "getcwd");
+ if (cwd == NULL) {
+ error = got_error_from_errno();
+ goto done;
+ }
if (path_prefix[0])
base = basename(path_prefix);
else
base = basename(repo_path);
- if (base == NULL)
- err(1, "basename");
+ if (base == NULL) {
+ error = got_error_from_errno();
+ goto done;
+ }
dotgit = strstr(base, ".git");
if (dotgit)
*dotgit = '\0';
if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
error = got_error_from_errno();
free(cwd);
- return error;
+ goto done;
}
free(cwd);
} else if (argc == 2) {
- repo_path = argv[0];
+ repo_path = realpath(argv[0], NULL);
+ if (repo_path == NULL) {
+ error = got_error_from_errno();
+ goto done;
+ }
worktree_path = realpath(argv[1], NULL);
- if (worktree_path == NULL)
- return got_error_from_errno();
+ if (worktree_path == NULL) {
+ error = got_error_from_errno();
+ goto done;
+ }
} else
usage_checkout();
error = got_repo_open(&repo, repo_path);
+ free(repo_path);
if (error != NULL)
goto done;
error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
@@ -235,6 +248,7 @@ cmd_checkout(int argc, char *argv[])
printf("checked out %s\n", worktree_path);
done:
+ free(repo_path);
free(worktree_path);
return error;
}
@@ -461,11 +475,14 @@ cmd_log(int argc, char *argv[])
if (repo_path == NULL)
err(1, "getcwd");
} else if (argc == 1) {
- repo_path = argv[0];
+ repo_path = realpath(argv[0], NULL);
+ if (repo_path == NULL)
+ return got_error_from_errno();
} else
usage_log();
error = got_repo_open(&repo, repo_path);
+ free(repo_path);
if (error != NULL)
return error;
@@ -625,13 +642,16 @@ cmd_diff(int argc, char *argv[])
obj_id_str1 = argv[0];
obj_id_str2 = argv[1];
} else if (argc == 3) {
- repo_path = argv[0];
+ repo_path = realpath(argv[0], NULL);
+ if (repo_path == NULL)
+ return got_error_from_errno();
obj_id_str1 = argv[1];
obj_id_str2 = argv[2];
} else
usage_diff();
error = got_repo_open(&repo, repo_path);
+ free(repo_path);
if (error != NULL)
goto done;