From 677a3c07f47f46dcbab4a3ee8a1e2aace6f999b0 Mon Sep 17 00:00:00 2001 From: nulltoken Date: Tue, 15 Mar 2011 22:07:01 +0100 Subject: [PATCH] Add failing test for issue 84 see https://github.com/libgit2/libgit2/issues#issue/84 --- src/fileops.c | 31 +++++++++++++++++++++++++++++++ src/fileops.h | 2 ++ tests/t12-repo.c | 22 ++++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/src/fileops.c b/src/fileops.c index a884b4002..4ed53bc97 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 bc636fc38..bd59011d5 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 a9a93d147..8b39e3902 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