Skip to content

Commit 6225140

Browse files
committed
fs_path: add length with suffix validation
1 parent 91246ee commit 6225140

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

src/fs_path.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,29 @@ bool git_fs_path_str_is_valid_ext(
17151715
return true;
17161716
}
17171717

1718+
int git_fs_path_validate_str_length_with_suffix(
1719+
git_str *path,
1720+
size_t suffix_len)
1721+
{
1722+
#ifdef GIT_WIN32
1723+
size_t utf8_len = git_utf8_char_length(path->ptr, path->size);
1724+
size_t total_len;
1725+
1726+
if (GIT_ADD_SIZET_OVERFLOW(&total_len, utf8_len, suffix_len) ||
1727+
total_len > MAX_PATH) {
1728+
1729+
git_error_set(GIT_ERROR_FILESYSTEM, "path too long: '%.*s'",
1730+
(int)path->size, path->ptr);
1731+
return -1;
1732+
}
1733+
#else
1734+
GIT_UNUSED(path);
1735+
GIT_UNUSED(suffix_len);
1736+
#endif
1737+
1738+
return 0;
1739+
}
1740+
17181741
#ifdef GIT_WIN32
17191742
GIT_INLINE(bool) should_validate_longpaths(git_repository *repo)
17201743
{

src/fs_path.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,10 @@ GIT_INLINE(bool) git_fs_path_str_is_valid(
677677
return git_fs_path_str_is_valid_ext(path, flags, NULL, NULL, NULL, NULL);
678678
}
679679

680+
extern int git_fs_path_validate_str_length_with_suffix(
681+
git_str *path,
682+
size_t suffix_len);
683+
680684
/**
681685
* Validate an on-disk path, taking into account that it will have a
682686
* suffix appended (eg, `.lock`).

src/refdb_fs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ GIT_INLINE(int) loose_path(
7777
if (git_str_joinpath(out, base, refname) < 0)
7878
return -1;
7979

80-
return git_fs_path_validate_filesystem_with_suffix(out->ptr, out->size,
80+
return git_fs_path_validate_str_length_with_suffix(out,
8181
CONST_STRLEN(".lock"));
8282
}
8383

src/repository.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ GIT_INLINE(int) validate_repo_path(git_str *path)
240240
CONST_STRLEN("objects/pack/pack-.pack.lock") +
241241
GIT_OID_HEXSZ;
242242

243-
return git_fs_path_validate_filesystem_with_suffix(
244-
path->ptr, path->size, suffix_len);
243+
return git_fs_path_validate_str_length_with_suffix(
244+
path, suffix_len);
245245
}
246246

247247
/*

0 commit comments

Comments
 (0)