Add failing test for issue 84 see https://github.com/libgit2/libgit2/issues#issue/84
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
diff --git a/src/fileops.c b/src/fileops.c
index a884b40..4ed53bc 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -633,3 +633,34 @@ int gitfo_cmp_path(const char *name1, int len1, int isdir1,
return 0;
}
+static void posixify_path(char *path)
+{
+ while (*path) {
+ if (*path == '\\')
+ *path = '/';
+
+ path++;
+ }
+}
+
+int gitfo_getcwd(char *buffer_out, size_t size)
+{
+ char *cwd_buffer;
+
+ assert(buffer_out && size > 0);
+
+#ifdef GIT_WIN32
+ cwd_buffer = _getcwd(buffer_out, size);
+#else
+ cwd_buffer = getcwd(buffer_out, size); //TODO: Fixme. Ensure the required headers are correctly included
+#endif
+
+ if (cwd_buffer == NULL)
+ return GIT_EOSERR;
+
+ posixify_path(buffer_out);
+
+ git__joinpath(buffer_out, buffer_out, ""); //Ensure the path ends with a trailing slash
+
+ return GIT_SUCCESS;
+}
diff --git a/src/fileops.h b/src/fileops.h
index bc636fc..bd59011 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -144,6 +144,8 @@ extern int gitfo_close_cached(gitfo_cache *ioc);
extern int gitfo_cmp_path(const char *name1, int len1, int isdir1,
const char *name2, int len2, int isdir2);
+extern int gitfo_getcwd(char *buffer_out, size_t size);
+
/**
* Clean up a provided absolute or relative directory path.
*
diff --git a/tests/t12-repo.c b/tests/t12-repo.c
index a9a93d1..8b39e39 100644
--- a/tests/t12-repo.c
+++ b/tests/t12-repo.c
@@ -162,11 +162,33 @@ BEGIN_TEST(init1, "initialize a bare repo")
must_pass(ensure_repository_init(TEMP_REPO_FOLDER_NS, BARE_REPOSITORY, NULL, path_repository, NULL));
END_TEST
+BEGIN_TEST(init2, "Initialize a bare repo with a relative path escaping out of the current working directory")
+ char path_repository[GIT_PATH_MAX];
+ char current_workdir[GIT_PATH_MAX];
+ const int mode = 0755; /* or 0777 ? */
+ git_repository* repo;
+
+ must_pass(gitfo_getcwd(current_workdir, sizeof(current_workdir)));
+
+ git__joinpath(path_repository, current_workdir, "a/b/c/");
+ must_pass(gitfo_mkdir_recurs(path_repository, mode));
+
+ must_pass(chdir(path_repository));
+
+ must_pass(git_repository_init(&repo, "../d/e.git", 1));
+ git_repository_free(repo);
+
+ must_pass(chdir(current_workdir));
+
+ git__joinpath(path_repository, current_workdir, "a/");
+ must_pass(rmdir_recurs(path_repository));
+END_TEST
BEGIN_SUITE(repository)
ADD_TEST(odb0);
ADD_TEST(odb1);
ADD_TEST(init0);
ADD_TEST(init1);
+ ADD_TEST(init2);
END_SUITE