Merge branch 'join-path-tests' of https://github.com/nulltoken/libgit2
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
diff --git a/src/util.c b/src/util.c
index e1f7099..67b74ee 100644
--- a/src/util.c
+++ b/src/util.c
@@ -207,6 +207,8 @@ char *git__joinpath(const char *path_a, const char *path_b)
int len_a, len_b;
char *path_new;
+ assert(path_a && path_b);
+
len_a = strlen(path_a);
len_b = strlen(path_b);
@@ -216,7 +218,7 @@ char *git__joinpath(const char *path_a, const char *path_b)
strcpy(path_new, path_a);
- if (path_new[len_a - 1] != '/')
+ if (len_a > 0 && len_b > 0 && path_new[len_a - 1] != '/')
path_new[len_a++] = '/';
if (path_b[0] == '/')
diff --git a/tests/t00-core.c b/tests/t00-core.c
index 7dd0995..f20aa9d 100644
--- a/tests/t00-core.c
+++ b/tests/t00-core.c
@@ -342,6 +342,36 @@ BEGIN_TEST("path", dir_path_prettifying)
must_fail(ensure_dir_path_normalized("/d1/.../d2", NULL));
END_TEST
+static int ensure_joinpath(const char *path_a, const char *path_b, const char *expected_path)
+{
+ int error = GIT_SUCCESS;
+ char* joined_path;
+
+ joined_path = git__joinpath(path_a, path_b);
+ if (joined_path == NULL)
+ return GIT_ERROR;
+
+ if (strcmp(joined_path, expected_path))
+ error = GIT_ERROR;
+
+ free(joined_path);
+
+ return error;
+}
+
+BEGIN_TEST("path", joinpath)
+ must_pass(ensure_joinpath("", "", ""));
+ must_pass(ensure_joinpath("", "a", "a"));
+ must_pass(ensure_joinpath("a", "", "a"));
+ must_pass(ensure_joinpath("a", "b", "a/b"));
+ must_pass(ensure_joinpath("/", "a", "/a"));
+ must_pass(ensure_joinpath("/", "", "/"));
+ must_pass(ensure_joinpath("/a", "/b", "/a/b"));
+ must_pass(ensure_joinpath("/a", "/b/", "/a/b/"));
+ must_pass(ensure_joinpath("/a/", "b/", "/a/b/"));
+ must_pass(ensure_joinpath("/a/", "/b/", "/a/b/"));
+END_TEST
+
typedef struct name_data {
int count; /* return count */
char *name; /* filename */
@@ -601,6 +631,7 @@ git_testsuite *libgit2_suite_core(void)
ADD_TEST(suite, "path", file_path_prettifying);
ADD_TEST(suite, "path", dir_path_prettifying);
+ ADD_TEST(suite, "path", joinpath);
ADD_TEST(suite, "dirent", dot);
ADD_TEST(suite, "dirent", sub);
diff --git a/tests/t06-index.c b/tests/t06-index.c
index 72702e8..f46dfb3 100644
--- a/tests/t06-index.c
+++ b/tests/t06-index.c
@@ -146,6 +146,8 @@ BEGIN_TEST("write", index_write_test)
must_pass(git_filelock_commit(&out_file));
git_index_free(index);
+
+ gitfo_unlink("index_rewrite");
END_TEST