handle non-const dirname in got_repo_open() and plug a memory leak ok naddy
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
diff --git a/lib/repository.c b/lib/repository.c
index 50af4bb..4ed0eaa 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -551,7 +551,7 @@ got_repo_open(struct got_repository **repop, const char *path,
{
struct got_repository *repo = NULL;
const struct got_error *err = NULL;
- char *abspath;
+ char *abspath, *repo_path = NULL;
int i, tried_root = 0;
*repop = NULL;
@@ -592,31 +592,33 @@ got_repo_open(struct got_repository **repop, const char *path,
if (err)
goto done;
- path = realpath(abspath, NULL);
- if (path == NULL) {
+ repo_path = realpath(abspath, NULL);
+ if (repo_path == NULL) {
err = got_error_from_errno2("realpath", abspath);
goto done;
}
- do {
- err = open_repo(repo, path);
+ for (;;) {
+ char *parent_path;
+
+ err = open_repo(repo, repo_path);
if (err == NULL)
break;
if (err->code != GOT_ERR_NOT_GIT_REPO)
goto done;
- if (path[0] == '/' && path[1] == '\0') {
+ if (repo_path[0] == '/' && repo_path[1] == '\0') {
if (tried_root) {
err = got_error(GOT_ERR_NOT_GIT_REPO);
goto done;
}
tried_root = 1;
}
- path = dirname(path);
- if (path == NULL) {
- err = got_error_from_errno2("dirname", path);
+ err = got_path_dirname(&parent_path, repo_path);
+ if (err)
goto done;
- }
- } while (path);
+ free(repo_path);
+ repo_path = parent_path;
+ }
err = read_gotconfig(repo);
if (err)
@@ -633,6 +635,7 @@ done:
else
*repop = repo;
free(abspath);
+ free(repo_path);
return err;
}