Skip to content

Commit f3bcadd

Browse files
committed
submodule: validate path lengths
Validate that working directory paths honor `core.longpaths` where appropriate. Paths to the submodule gitdirs must always honor the operating system length restrictions; `core.longpaths` does not affect gitdir paths.
1 parent b457fe2 commit f3bcadd

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

src/submodule.c

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,10 @@ int git_submodule__lookup_with_cache(
380380
/* If it's not configured, we still check if there's a repo at the path */
381381
if (git_repository_workdir(repo)) {
382382
git_buf path = GIT_BUF_INIT;
383-
if (git_buf_join3(&path,
384-
'/', git_repository_workdir(repo), name, DOT_GIT) < 0)
383+
if (git_buf_join3(&path, '/',
384+
git_repository_workdir(repo),
385+
name, DOT_GIT) < 0 ||
386+
git_path_validate_workdir_buf(NULL, &path) < 0)
385387
return -1;
386388

387389
if (git_path_exists(path.ptr))
@@ -553,10 +555,10 @@ int git_submodule__map(git_repository *repo, git_strmap *map)
553555
int error = 0;
554556
git_index *idx = NULL;
555557
git_tree *head = NULL;
556-
const char *wd = NULL;
557558
git_buf path = GIT_BUF_INIT;
558559
git_submodule *sm;
559560
git_config *mods = NULL;
561+
bool has_workdir;
560562

561563
GIT_ASSERT_ARG(repo);
562564
GIT_ASSERT_ARG(map);
@@ -567,12 +569,14 @@ int git_submodule__map(git_repository *repo, git_strmap *map)
567569
if (git_repository_head_tree(&head, repo) < 0)
568570
git_error_clear();
569571

570-
wd = git_repository_workdir(repo);
571-
if (wd && (error = git_buf_joinpath(&path, wd, GIT_MODULES_FILE)) < 0)
572+
has_workdir = git_repository_workdir(repo) != NULL;
573+
574+
if (has_workdir &&
575+
(error = git_repository_workdir_path(&path, repo, GIT_MODULES_FILE)) < 0)
572576
goto cleanup;
573577

574578
/* add submodule information from .gitmodules */
575-
if (wd) {
579+
if (has_workdir) {
576580
lfc_data data = { 0 };
577581
data.map = map;
578582
data.repo = repo;
@@ -599,7 +603,7 @@ int git_submodule__map(git_repository *repo, git_strmap *map)
599603
goto cleanup;
600604
}
601605
/* shallow scan submodules in work tree as needed */
602-
if (wd) {
606+
if (has_workdir) {
603607
git_strmap_foreach_value(map, sm, {
604608
submodule_load_from_wd_lite(sm);
605609
});
@@ -683,7 +687,7 @@ static int submodule_repo_init(
683687
git_repository_init_options initopt = GIT_REPOSITORY_INIT_OPTIONS_INIT;
684688
git_repository *subrepo = NULL;
685689

686-
error = git_buf_joinpath(&workdir, git_repository_workdir(parent_repo), path);
690+
error = git_repository_workdir_path(&workdir, parent_repo, path);
687691
if (error < 0)
688692
goto cleanup;
689693

@@ -790,7 +794,7 @@ int git_submodule_add_setup(
790794

791795
/* init submodule repository and add origin remote as needed */
792796

793-
error = git_buf_joinpath(&name, git_repository_workdir(repo), path);
797+
error = git_repository_workdir_path(&name, repo, path);
794798
if (error < 0)
795799
goto cleanup;
796800

@@ -896,10 +900,9 @@ int git_submodule_clone(git_repository **out, git_submodule *submodule, const gi
896900
opts.remote_cb = clone_return_origin;
897901
opts.remote_cb_payload = submodule;
898902

899-
git_buf_puts(&rel_path, git_repository_workdir(git_submodule_owner(submodule)));
900-
git_buf_joinpath(&rel_path, git_buf_cstr(&rel_path), git_submodule_path(submodule));
901-
902-
GIT_ERROR_CHECK_ALLOC_BUF(&rel_path);
903+
error = git_repository_workdir_path(&rel_path, git_submodule_owner(submodule), git_submodule_path(submodule));
904+
if (error < 0)
905+
goto cleanup;
903906

904907
error = git_clone__submodule(&clone, git_submodule_url(submodule), git_buf_cstr(&rel_path), &opts);
905908
if (error < 0)
@@ -946,9 +949,8 @@ int git_submodule_add_to_index(git_submodule *sm, int write_index)
946949
sm->flags = sm->flags & ~GIT_SUBMODULE_STATUS__WD_OID_VALID;
947950

948951
if ((error = git_repository_index__weakptr(&index, sm->repo)) < 0 ||
949-
(error = git_buf_joinpath(
950-
&path, git_repository_workdir(sm->repo), sm->path)) < 0 ||
951-
(error = git_submodule_open(&sm_repo, sm)) < 0)
952+
(error = git_repository_workdir_path(&path, sm->repo, sm->path)) < 0 ||
953+
(error = git_submodule_open(&sm_repo, sm)) < 0)
952954
goto cleanup;
953955

954956
/* read stat information for submodule working directory */
@@ -1237,7 +1239,7 @@ static int submodule_repo_create(
12371239
GIT_REPOSITORY_INIT_RELATIVE_GITLINK;
12381240

12391241
/* Workdir: path to sub-repo working directory */
1240-
error = git_buf_joinpath(&workdir, git_repository_workdir(parent_repo), path);
1242+
error = git_repository_workdir_path(&workdir, parent_repo, path);
12411243
if (error < 0)
12421244
goto cleanup;
12431245

@@ -1534,8 +1536,7 @@ static int git_submodule__open(
15341536

15351537
wd = git_repository_workdir(sm->repo);
15361538

1537-
if (git_buf_joinpath(&path, wd, sm->path) < 0 ||
1538-
git_buf_joinpath(&path, path.ptr, DOT_GIT) < 0)
1539+
if (git_buf_join3(&path, '/', wd, sm->path, DOT_GIT) < 0)
15391540
return -1;
15401541

15411542
sm->flags = sm->flags &
@@ -2080,7 +2081,7 @@ static int submodule_load_from_wd_lite(git_submodule *sm)
20802081
{
20812082
git_buf path = GIT_BUF_INIT;
20822083

2083-
if (git_buf_joinpath(&path, git_repository_workdir(sm->repo), sm->path) < 0)
2084+
if (git_repository_workdir_path(&path, sm->repo, sm->path) < 0)
20842085
return -1;
20852086

20862087
if (git_path_isdir(path.ptr))
@@ -2100,15 +2101,14 @@ static int submodule_load_from_wd_lite(git_submodule *sm)
21002101
*/
21012102
static int gitmodules_snapshot(git_config **snap, git_repository *repo)
21022103
{
2103-
const char *workdir = git_repository_workdir(repo);
21042104
git_config *mods = NULL;
21052105
git_buf path = GIT_BUF_INIT;
21062106
int error;
21072107

2108-
if (!workdir)
2108+
if (git_repository_workdir(repo) == NULL)
21092109
return GIT_ENOTFOUND;
21102110

2111-
if ((error = git_buf_joinpath(&path, workdir, GIT_MODULES_FILE)) < 0)
2111+
if ((error = git_repository_workdir_path(&path, repo, GIT_MODULES_FILE)) < 0)
21122112
return error;
21132113

21142114
if ((error = git_config_open_ondisk(&mods, path.ptr)) < 0)
@@ -2132,12 +2132,11 @@ static git_config_backend *open_gitmodules(
21322132
git_repository *repo,
21332133
int okay_to_create)
21342134
{
2135-
const char *workdir = git_repository_workdir(repo);
21362135
git_buf path = GIT_BUF_INIT;
21372136
git_config_backend *mods = NULL;
21382137

2139-
if (workdir != NULL) {
2140-
if (git_buf_joinpath(&path, workdir, GIT_MODULES_FILE) != 0)
2138+
if (git_repository_workdir(repo) != NULL) {
2139+
if (git_repository_workdir_path(&path, repo, GIT_MODULES_FILE) != 0)
21412140
return NULL;
21422141

21432142
if (okay_to_create || git_path_isfile(path.ptr)) {
@@ -2250,8 +2249,9 @@ static int get_url_base(git_buf *url, git_repository *repo)
22502249
if ((error = git_worktree_open_from_repository(&wt, repo)) < 0)
22512250
goto out;
22522251
error = git_buf_sets(url, wt->parent_path);
2253-
} else
2252+
} else {
22542253
error = git_buf_sets(url, git_repository_workdir(repo));
2254+
}
22552255

22562256
out:
22572257
git_remote_free(remote);

0 commit comments

Comments
 (0)