Skip to content

Commit 8ad5998

Browse files
committed
Fix memory sanitizer failures in tests
1 parent 6c035b5 commit 8ad5998

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

tests/remote/fetch.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,52 @@
55

66
static git_repository *repo1;
77
static git_repository *repo2;
8+
static char* repo1_path;
9+
static char* repo2_path;
810

911
static const char *REPO1_REFNAME = "refs/heads/main";
1012
static const char *REPO2_REFNAME = "refs/remotes/repo1/main";
1113
static char *FORCE_FETCHSPEC = "+refs/heads/main:refs/remotes/repo1/main";
1214
static char *NON_FORCE_FETCHSPEC = "refs/heads/main:refs/remotes/repo1/main";
1315

1416
char* strip_trailing_slash(char *path) {
15-
char *result = NULL;
1617
if (path[strlen(path) - 1] == '/') {
17-
result = (char *) malloc(strlen(path) - 1);
18+
char* result = (char *) calloc(strlen(path) - 1, sizeof(char));
1819
memcpy(result, path, strlen(path) - 1);
20+
return result;
1921
} else {
20-
result = (char *) malloc(strlen(path));
22+
char* result = (char *) calloc(strlen(path), sizeof(char));
2123
strncpy(result, path, strlen(path));
24+
return result;
2225
}
23-
return result;
2426
}
2527

2628

2729
void test_remote_fetch__initialize(void) {
2830
git_config *c;
29-
git_buf repo1_path = GIT_BUF_INIT;
30-
git_buf repo2_path = GIT_BUF_INIT;
31+
git_buf repo1_path_buf = GIT_BUF_INIT;
32+
git_buf repo2_path_buf = GIT_BUF_INIT;
3133
const char *sandbox = clar_sandbox_path();
3234

33-
cl_git_pass(git_buf_join(&repo1_path, '/', sandbox, "fetchtest_repo1"));
34-
cl_git_pass(git_repository_init(&repo1, repo1_path.ptr, true));
35+
cl_git_pass(git_buf_join(&repo1_path_buf, '/', sandbox, "fetchtest_repo1"));
36+
repo1_path = calloc(repo1_path_buf.size, sizeof(char));
37+
git_buf_copy_cstr(repo1_path, repo1_path_buf.size, &repo1_path_buf);
38+
cl_git_pass(git_repository_init(&repo1, repo1_path, true));
3539

36-
cl_git_pass(git_buf_join(&repo2_path, '/', sandbox, "fetchtest_repo2"));
37-
cl_git_pass(git_repository_init(&repo2, repo2_path.ptr, true));
40+
cl_git_pass(git_buf_join(&repo2_path_buf, '/', sandbox, "fetchtest_repo2"));
41+
repo2_path = calloc(repo2_path_buf.size, sizeof(char));
42+
git_buf_copy_cstr(repo2_path, repo2_path_buf.size, &repo2_path_buf);
43+
cl_git_pass(git_repository_init(&repo2, repo2_path, true));
3844

3945
cl_git_pass(git_repository_config(&c, repo1));
4046
cl_git_pass(git_config_set_string(c, "user.email", "some@email"));
4147
cl_git_pass(git_config_set_string(c, "user.name", "some@name"));
4248
git_config_free(c);
43-
git_buf_dispose(&repo1_path);
44-
git_buf_dispose(&repo2_path);
49+
git_buf_dispose(&repo1_path_buf);
50+
git_buf_dispose(&repo2_path_buf);
4551
}
4652

4753
void test_remote_fetch__cleanup(void) {
48-
char *repo1_path = strip_trailing_slash(repo1->gitdir);
49-
char *repo2_path = strip_trailing_slash(repo2->gitdir);
50-
5154
git_repository_free(repo1);
5255
git_repository_free(repo2);
5356

0 commit comments

Comments
 (0)