Skip to content

Commit 02c80ad

Browse files
committed
path: accept the name length as a parameter
We may take in names from the middle of a string so we want the caller to let us know how long the path component is that we should be checking.
1 parent a145f2b commit 02c80ad

File tree

3 files changed

+47
-40
lines changed

3 files changed

+47
-40
lines changed

src/path.c

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,10 +1635,8 @@ GIT_INLINE(bool) only_spaces_and_dots(const char *path)
16351635
return true;
16361636
}
16371637

1638-
GIT_INLINE(bool) verify_dotgit_ntfs_generic(const char *name, const char *dotgit_name, const char *shortname_pfix)
1638+
GIT_INLINE(bool) verify_dotgit_ntfs_generic(const char *name, size_t len, const char *dotgit_name, size_t dotgit_len, const char *shortname_pfix)
16391639
{
1640-
size_t len = strlen(name);
1641-
size_t dotgit_len = strlen(dotgit_name);
16421640
int i, saw_tilde;
16431641

16441642
if (name[0] == '.' && len >= dotgit_len &&
@@ -1842,64 +1840,64 @@ int git_path_normalize_slashes(git_buf *out, const char *path)
18421840
return 0;
18431841
}
18441842

1845-
static int verify_dotgit_generic(const char *name, const char *dotgit_name, const char *shortname_pfix)
1843+
static int verify_dotgit_generic(const char *name, size_t len, const char *dotgit_name, size_t dotgit_len, const char *shortname_pfix)
18461844
{
1847-
if (!verify_dotgit_ntfs_generic(name, dotgit_name, shortname_pfix))
1845+
if (!verify_dotgit_ntfs_generic(name, len, dotgit_name, dotgit_len, shortname_pfix))
18481846
return false;
18491847

1850-
return verify_dotgit_hfs_generic(name, strlen(name), dotgit_name, strlen(dotgit_name));
1848+
return verify_dotgit_hfs_generic(name, len, dotgit_name, dotgit_len);
18511849
}
18521850

1853-
int git_path_is_ntfs_dotgit_modules(const char *name)
1851+
int git_path_is_ntfs_dotgit_modules(const char *name, size_t len)
18541852
{
1855-
return !verify_dotgit_ntfs_generic(name, "gitmodules", "gi7eba");
1853+
return !verify_dotgit_ntfs_generic(name, len, "gitmodules", CONST_STRLEN("gitmodules"), "gi7eba");
18561854
}
18571855

1858-
int git_path_is_hfs_dotgit_modules(const char *name)
1856+
int git_path_is_hfs_dotgit_modules(const char *name, size_t len)
18591857
{
1860-
return !verify_dotgit_hfs_generic(name, strlen(name), "gitmodules", CONST_STRLEN("gitmodules"));
1858+
return !verify_dotgit_hfs_generic(name, len, "gitmodules", CONST_STRLEN("gitmodules"));
18611859
}
18621860

1863-
int git_path_is_dotgit_modules(const char *name)
1861+
int git_path_is_dotgit_modules(const char *name, size_t len)
18641862
{
1865-
if (git_path_is_hfs_dotgit_modules(name))
1863+
if (git_path_is_hfs_dotgit_modules(name, len))
18661864
return 1;
18671865

1868-
return git_path_is_ntfs_dotgit_modules(name);
1866+
return git_path_is_ntfs_dotgit_modules(name, len);
18691867
}
18701868

1871-
int git_path_is_ntfs_dotgit_ignore(const char *name)
1869+
int git_path_is_ntfs_dotgit_ignore(const char *name, size_t len)
18721870
{
1873-
return !verify_dotgit_ntfs_generic(name, "gitignore", "gi250a");
1871+
return !verify_dotgit_ntfs_generic(name, len, "gitignore", CONST_STRLEN("gitignore"), "gi250a");
18741872
}
18751873

1876-
int git_path_is_hfs_dotgit_ignore(const char *name)
1874+
int git_path_is_hfs_dotgit_ignore(const char *name, size_t len)
18771875
{
1878-
return !verify_dotgit_hfs_generic(name, strlen(name), "gitignore", CONST_STRLEN("gitignore"));
1876+
return !verify_dotgit_hfs_generic(name, len, "gitignore", CONST_STRLEN("gitignore"));
18791877
}
18801878

1881-
int git_path_is_dotgit_ignore(const char *name)
1879+
int git_path_is_dotgit_ignore(const char *name, size_t len)
18821880
{
1883-
if (git_path_is_hfs_dotgit_ignore(name))
1881+
if (git_path_is_hfs_dotgit_ignore(name, len))
18841882
return 1;
18851883

1886-
return git_path_is_ntfs_dotgit_ignore(name);
1884+
return git_path_is_ntfs_dotgit_ignore(name, len);
18871885
}
18881886

1889-
int git_path_is_hfs_dotgit_attributes(const char *name)
1887+
int git_path_is_hfs_dotgit_attributes(const char *name, size_t len)
18901888
{
1891-
return !verify_dotgit_hfs_generic(name, strlen(name), "gitattributes", CONST_STRLEN("gitattributes"));
1889+
return !verify_dotgit_hfs_generic(name, len, "gitattributes", CONST_STRLEN("gitattributes"));
18921890
}
18931891

1894-
int git_path_is_ntfs_dotgit_attributes(const char *name)
1892+
int git_path_is_ntfs_dotgit_attributes(const char *name, size_t len)
18951893
{
1896-
return !verify_dotgit_ntfs_generic(name, "gitattributes", "gi7d29");
1894+
return !verify_dotgit_ntfs_generic(name, len, "gitattributes", CONST_STRLEN("gitattributes"), "gi7d29");
18971895
}
18981896

1899-
int git_path_is_dotgit_attributes(const char *name)
1897+
int git_path_is_dotgit_attributes(const char *name, size_t len)
19001898
{
1901-
if (git_path_is_hfs_dotgit_attributes(name))
1899+
if (git_path_is_hfs_dotgit_attributes(name, len))
19021900
return 1;
19031901

1904-
return git_path_is_ntfs_dotgit_attributes(name);
1902+
return git_path_is_ntfs_dotgit_attributes(name, len);
19051903
}

src/path.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -648,63 +648,72 @@ int git_path_normalize_slashes(git_buf *out, const char *path);
648648
* Check whether a path component corresponds to a .gitmodules file
649649
*
650650
* @param name the path component to check
651+
* @param len the length of `name`
651652
*/
652-
extern int git_path_is_dotgit_modules(const char *name);
653+
extern int git_path_is_dotgit_modules(const char *name, size_t len);
653654

654655
/**
655656
* Check whether a path component corresponds to a .gitmodules file in NTFS
656657
*
657658
* @param name the path component to check
659+
* @param len the length of `name`
658660
*/
659-
extern int git_path_is_ntfs_dotgit_modules(const char *name);
661+
extern int git_path_is_ntfs_dotgit_modules(const char *name, size_t len);
660662

661663
/**
662664
* Check whether a path component corresponds to a .gitmodules file in HFS+
663665
*
664666
* @param name the path component to check
667+
* @param len the length of `name`
665668
*/
666-
extern int git_path_is_hfs_dotgit_modules(const char *name);
669+
extern int git_path_is_hfs_dotgit_modules(const char *name, size_t len);
667670

668671
/**
669672
* Check whether a path component corresponds to a .gitignore file
670673
*
671674
* @param name the path component to check
675+
* @param len the length of `name`
672676
*/
673-
extern int git_path_is_dotgit_ignore(const char *name);
677+
extern int git_path_is_dotgit_ignore(const char *name, size_t len);
674678

675679
/**
676680
* Check whether a path component corresponds to a .gitignore file in NTFS
677681
*
678682
* @param name the path component to check
683+
* @param len the length of `name`
679684
*/
680-
extern int git_path_is_ntfs_dotgit_ignore(const char *name);
685+
extern int git_path_is_ntfs_dotgit_ignore(const char *name, size_t len);
681686

682687
/**
683688
* Check whether a path component corresponds to a .gitignore file in HFS+
684689
*
685690
* @param name the path component to check
691+
* @param len the length of `name`
686692
*/
687-
extern int git_path_is_hfs_dotgit_ignore(const char *name);
693+
extern int git_path_is_hfs_dotgit_ignore(const char *name, size_t len);
688694

689695
/**
690696
* Check whether a path component corresponds to a .gitignore file
691697
*
692698
* @param name the path component to check
699+
* @param len the length of `name`
693700
*/
694-
extern int git_path_is_dotgit_attributes(const char *name);
701+
extern int git_path_is_dotgit_attributes(const char *name, size_t len);
695702

696703
/**
697704
* Check whether a path component corresponds to a .gitattributes file in NTFS
698705
*
699706
* @param name the path component to check
707+
* @param len the length of `name`
700708
*/
701-
extern int git_path_is_ntfs_dotgit_attributes(const char *name);
709+
extern int git_path_is_ntfs_dotgit_attributes(const char *name, size_t len);
702710

703711
/**
704712
* Check whether a path component corresponds to a .gitattributes file in HFS+
705713
*
706714
* @param name the path component to check
715+
* @param len the length of `name`
707716
*/
708-
extern int git_path_is_hfs_dotgit_attributes(const char *name);
717+
extern int git_path_is_hfs_dotgit_attributes(const char *name, size_t len);
709718

710719
#endif

tests/path/dotgit.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,18 @@ static char *gitmodules_not_altnames[] = {
9090
void test_path_dotgit__dotgit_modules(void)
9191
{
9292
size_t i;
93-
cl_assert_equal_i(1, git_path_is_dotgit_modules(".gitmodules"));
94-
cl_assert_equal_i(1, git_path_is_dotgit_modules(".git\xe2\x80\x8cmodules"));
93+
cl_assert_equal_i(1, git_path_is_dotgit_modules(".gitmodules", strlen(".gitmodules")));
94+
cl_assert_equal_i(1, git_path_is_dotgit_modules(".git\xe2\x80\x8cmodules", strlen(".git\xe2\x80\x8cmodules")));
9595

9696
for (i = 0; i < ARRAY_SIZE(gitmodules_altnames); i++) {
9797
const char *name = gitmodules_altnames[i];
98-
if (!git_path_is_dotgit_modules(name))
98+
if (!git_path_is_dotgit_modules(name, strlen(name)))
9999
cl_fail(name);
100100
}
101101

102102
for (i = 0; i < ARRAY_SIZE(gitmodules_not_altnames); i++) {
103103
const char *name = gitmodules_not_altnames[i];
104-
if (git_path_is_dotgit_modules(name))
104+
if (git_path_is_dotgit_modules(name, strlen(name)))
105105
cl_fail(name);
106106
}
107107

0 commit comments

Comments
 (0)