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 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
diff --git a/src/path.c b/src/path.c
index 560cf86..816a359 100644
--- a/src/path.c
+++ b/src/path.c
@@ -1634,10 +1634,8 @@ GIT_INLINE(bool) only_spaces_and_dots(const char *path)
return true;
}
-GIT_INLINE(bool) verify_dotgit_ntfs_generic(const char *name, const char *dotgit_name, const char *shortname_pfix)
+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)
{
- size_t len = strlen(name);
- size_t dotgit_len = strlen(dotgit_name);
int i, saw_tilde;
if (name[0] == '.' && len >= dotgit_len &&
@@ -1841,64 +1839,64 @@ int git_path_normalize_slashes(git_buf *out, const char *path)
return 0;
}
-static int verify_dotgit_generic(const char *name, const char *dotgit_name, const char *shortname_pfix)
+static int verify_dotgit_generic(const char *name, size_t len, const char *dotgit_name, size_t dotgit_len, const char *shortname_pfix)
{
- if (!verify_dotgit_ntfs_generic(name, dotgit_name, shortname_pfix))
+ if (!verify_dotgit_ntfs_generic(name, len, dotgit_name, dotgit_len, shortname_pfix))
return false;
- return verify_dotgit_hfs_generic(name, strlen(name), dotgit_name, strlen(dotgit_name));
+ return verify_dotgit_hfs_generic(name, len, dotgit_name, dotgit_len);
}
-int git_path_is_ntfs_dotgit_modules(const char *name)
+int git_path_is_ntfs_dotgit_modules(const char *name, size_t len)
{
- return !verify_dotgit_ntfs_generic(name, "gitmodules", "gi7eba");
+ return !verify_dotgit_ntfs_generic(name, len, "gitmodules", CONST_STRLEN("gitmodules"), "gi7eba");
}
-int git_path_is_hfs_dotgit_modules(const char *name)
+int git_path_is_hfs_dotgit_modules(const char *name, size_t len)
{
- return !verify_dotgit_hfs_generic(name, strlen(name), "gitmodules", CONST_STRLEN("gitmodules"));
+ return !verify_dotgit_hfs_generic(name, len, "gitmodules", CONST_STRLEN("gitmodules"));
}
-int git_path_is_dotgit_modules(const char *name)
+int git_path_is_dotgit_modules(const char *name, size_t len)
{
- if (git_path_is_hfs_dotgit_modules(name))
+ if (git_path_is_hfs_dotgit_modules(name, len))
return 1;
- return git_path_is_ntfs_dotgit_modules(name);
+ return git_path_is_ntfs_dotgit_modules(name, len);
}
-int git_path_is_ntfs_dotgit_ignore(const char *name)
+int git_path_is_ntfs_dotgit_ignore(const char *name, size_t len)
{
- return !verify_dotgit_ntfs_generic(name, "gitignore", "gi250a");
+ return !verify_dotgit_ntfs_generic(name, len, "gitignore", CONST_STRLEN("gitignore"), "gi250a");
}
-int git_path_is_hfs_dotgit_ignore(const char *name)
+int git_path_is_hfs_dotgit_ignore(const char *name, size_t len)
{
- return !verify_dotgit_hfs_generic(name, strlen(name), "gitignore", CONST_STRLEN("gitignore"));
+ return !verify_dotgit_hfs_generic(name, len, "gitignore", CONST_STRLEN("gitignore"));
}
-int git_path_is_dotgit_ignore(const char *name)
+int git_path_is_dotgit_ignore(const char *name, size_t len)
{
- if (git_path_is_hfs_dotgit_ignore(name))
+ if (git_path_is_hfs_dotgit_ignore(name, len))
return 1;
- return git_path_is_ntfs_dotgit_ignore(name);
+ return git_path_is_ntfs_dotgit_ignore(name, len);
}
-int git_path_is_hfs_dotgit_attributes(const char *name)
+int git_path_is_hfs_dotgit_attributes(const char *name, size_t len)
{
- return !verify_dotgit_hfs_generic(name, strlen(name), "gitattributes", CONST_STRLEN("gitattributes"));
+ return !verify_dotgit_hfs_generic(name, len, "gitattributes", CONST_STRLEN("gitattributes"));
}
-int git_path_is_ntfs_dotgit_attributes(const char *name)
+int git_path_is_ntfs_dotgit_attributes(const char *name, size_t len)
{
- return !verify_dotgit_ntfs_generic(name, "gitattributes", "gi7d29");
+ return !verify_dotgit_ntfs_generic(name, len, "gitattributes", CONST_STRLEN("gitattributes"), "gi7d29");
}
-int git_path_is_dotgit_attributes(const char *name)
+int git_path_is_dotgit_attributes(const char *name, size_t len)
{
- if (git_path_is_hfs_dotgit_attributes(name))
+ if (git_path_is_hfs_dotgit_attributes(name, len))
return 1;
- return git_path_is_ntfs_dotgit_attributes(name);
+ return git_path_is_ntfs_dotgit_attributes(name, len);
}
diff --git a/src/path.h b/src/path.h
index 553608a..8609388 100644
--- a/src/path.h
+++ b/src/path.h
@@ -634,63 +634,72 @@ int git_path_normalize_slashes(git_buf *out, const char *path);
* Check whether a path component corresponds to a .gitmodules file
*
* @param name the path component to check
+ * @param len the length of `name`
*/
-extern int git_path_is_dotgit_modules(const char *name);
+extern int git_path_is_dotgit_modules(const char *name, size_t len);
/**
* Check whether a path component corresponds to a .gitmodules file in NTFS
*
* @param name the path component to check
+ * @param len the length of `name`
*/
-extern int git_path_is_ntfs_dotgit_modules(const char *name);
+extern int git_path_is_ntfs_dotgit_modules(const char *name, size_t len);
/**
* Check whether a path component corresponds to a .gitmodules file in HFS+
*
* @param name the path component to check
+ * @param len the length of `name`
*/
-extern int git_path_is_hfs_dotgit_modules(const char *name);
+extern int git_path_is_hfs_dotgit_modules(const char *name, size_t len);
/**
* Check whether a path component corresponds to a .gitignore file
*
* @param name the path component to check
+ * @param len the length of `name`
*/
-extern int git_path_is_dotgit_ignore(const char *name);
+extern int git_path_is_dotgit_ignore(const char *name, size_t len);
/**
* Check whether a path component corresponds to a .gitignore file in NTFS
*
* @param name the path component to check
+ * @param len the length of `name`
*/
-extern int git_path_is_ntfs_dotgit_ignore(const char *name);
+extern int git_path_is_ntfs_dotgit_ignore(const char *name, size_t len);
/**
* Check whether a path component corresponds to a .gitignore file in HFS+
*
* @param name the path component to check
+ * @param len the length of `name`
*/
-extern int git_path_is_hfs_dotgit_ignore(const char *name);
+extern int git_path_is_hfs_dotgit_ignore(const char *name, size_t len);
/**
* Check whether a path component corresponds to a .gitignore file
*
* @param name the path component to check
+ * @param len the length of `name`
*/
-extern int git_path_is_dotgit_attributes(const char *name);
+extern int git_path_is_dotgit_attributes(const char *name, size_t len);
/**
* Check whether a path component corresponds to a .gitattributes file in NTFS
*
* @param name the path component to check
+ * @param len the length of `name`
*/
-extern int git_path_is_ntfs_dotgit_attributes(const char *name);
+extern int git_path_is_ntfs_dotgit_attributes(const char *name, size_t len);
/**
* Check whether a path component corresponds to a .gitattributes file in HFS+
*
* @param name the path component to check
+ * @param len the length of `name`
*/
-extern int git_path_is_hfs_dotgit_attributes(const char *name);
+extern int git_path_is_hfs_dotgit_attributes(const char *name, size_t len);
#endif
diff --git a/tests/path/dotgit.c b/tests/path/dotgit.c
index 7a011d4..038e849 100644
--- a/tests/path/dotgit.c
+++ b/tests/path/dotgit.c
@@ -90,18 +90,18 @@ static char *gitmodules_not_altnames[] = {
void test_path_dotgit__dotgit_modules(void)
{
size_t i;
- cl_assert_equal_i(1, git_path_is_dotgit_modules(".gitmodules"));
- cl_assert_equal_i(1, git_path_is_dotgit_modules(".git\xe2\x80\x8cmodules"));
+ cl_assert_equal_i(1, git_path_is_dotgit_modules(".gitmodules", strlen(".gitmodules")));
+ cl_assert_equal_i(1, git_path_is_dotgit_modules(".git\xe2\x80\x8cmodules", strlen(".git\xe2\x80\x8cmodules")));
for (i = 0; i < ARRAY_SIZE(gitmodules_altnames); i++) {
const char *name = gitmodules_altnames[i];
- if (!git_path_is_dotgit_modules(name))
+ if (!git_path_is_dotgit_modules(name, strlen(name)))
cl_fail(name);
}
for (i = 0; i < ARRAY_SIZE(gitmodules_not_altnames); i++) {
const char *name = gitmodules_not_altnames[i];
- if (git_path_is_dotgit_modules(name))
+ if (git_path_is_dotgit_modules(name, strlen(name)))
cl_fail(name);
}