deps/libgit2/src/libgit2/path.c

filter: Statementstmt 34.6% 54 / 156 Branchbran 26.0% 36 / 138 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 30.6% 90 / 294 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« deps/libgit2/src/libgit2/xdiff/xdiffi.c ↑ Summary deps/libgit2/src/libgit2/branch.c »
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7  
8 #include "path.h"
9  
10 #include "repository.h"
11 #include "fs_path.h"
12 #include "utf8.h"
13  
14 typedef struct {
15 git_repository *repo;
16 uint16_t file_mode;
17 unsigned int flags;
18 } repository_path_validate_data;
19  
20 0 static int32_t next_hfs_char(const char **in, size_t *len)
21 {
22 0 ▶ while (*len) {
Branch: 1 branch
BranchTrueFalseTotal
while (*len) { 0 0 0
23 uint32_t codepoint;
24 0 int cp_len = git_utf8_iterate(&codepoint, *in, *len);
25 0 ▶ if (cp_len < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (cp_len < 0) 0 0 0
26 0 return -1;
27  
28 0 (*in) += cp_len;
29 0 (*len) -= cp_len;
30  
31 /* these code points are ignored completely */
32 0 ▶ switch (codepoint) {
Branch: 1 branch
BranchTrueFalseTotal
switch (codepoint) { 0 0 0
33 0 case 0x200c: /* ZERO WIDTH NON-JOINER */
34 case 0x200d: /* ZERO WIDTH JOINER */
35 case 0x200e: /* LEFT-TO-RIGHT MARK */
36 case 0x200f: /* RIGHT-TO-LEFT MARK */
37 case 0x202a: /* LEFT-TO-RIGHT EMBEDDING */
38 case 0x202b: /* RIGHT-TO-LEFT EMBEDDING */
39 case 0x202c: /* POP DIRECTIONAL FORMATTING */
40 case 0x202d: /* LEFT-TO-RIGHT OVERRIDE */
41 case 0x202e: /* RIGHT-TO-LEFT OVERRIDE */
42 case 0x206a: /* INHIBIT SYMMETRIC SWAPPING */
43 case 0x206b: /* ACTIVATE SYMMETRIC SWAPPING */
44 case 0x206c: /* INHIBIT ARABIC FORM SHAPING */
45 case 0x206d: /* ACTIVATE ARABIC FORM SHAPING */
46 case 0x206e: /* NATIONAL DIGIT SHAPES */
47 case 0x206f: /* NOMINAL DIGIT SHAPES */
48 case 0xfeff: /* ZERO WIDTH NO-BREAK SPACE */
49 0 continue;
50 }
51  
52 /* fold into lowercase -- this will only fold characters in
53 * the ASCII range, which is perfectly fine, because the
54 * git folder name can only be composed of ascii characters
55 */
56 0 return git__tolower((int)codepoint);
57 }
58 0 return 0; /* NULL byte -- end of string */
59 }
60  
61 0 static bool validate_dotgit_hfs_generic(
62 const char *path,
63 size_t len,
64 const char *needle,
65 size_t needle_len)
66 {
67 size_t i;
68 char c;
69  
70 0 ▶ if (next_hfs_char(&path, &len) != '.')
Branch: 1 branch
BranchTrueFalseTotal
if (next_hfs_char(&path, &len) != '.') 0 0 0
71 0 return true;
72  
73 0 ▶ for (i = 0; i < needle_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < needle_len; i++) { 0 0 0
74 0 c = next_hfs_char(&path, &len);
75 0 ▶ if (c != needle[i])
Branch: 1 branch
BranchTrueFalseTotal
if (c != needle[i]) 0 0 0
76 0 return true;
77 }
78  
79 0 ▶ if (next_hfs_char(&path, &len) != '\0')
Branch: 1 branch
BranchTrueFalseTotal
if (next_hfs_char(&path, &len) != '\0') 0 0 0
80 0 return true;
81  
82 0 return false;
83 }
84  
85 0 static bool validate_dotgit_hfs(const char *path, size_t len)
86 {
87 0 return validate_dotgit_hfs_generic(path, len, "git", CONST_STRLEN("git"));
88 }
89  
90 535 GIT_INLINE(bool) validate_dotgit_ntfs(
91 git_repository *repo,
92 const char *path,
93 size_t len)
94 {
95 535 git_str *reserved = git_repository__reserved_names_win32;
96 535 size_t reserved_len = git_repository__reserved_names_win32_len;
97 535 size_t start = 0, i;
98  
99 535 ▶ if (repo)
Branch: 1 branch
BranchTrueFalseTotal
if (repo) 370 165 535
100 370 git_repository__reserved_names(&reserved, &reserved_len, repo, true);
101  
102 1579 ▶ for (i = 0; i < reserved_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < reserved_len; i++) { 1057 522 1579
103 1057 git_str *r = &reserved[i];
104  
105 1057 ▶ if (len >= r->size &&
Branch: 1 branch
BranchTrueFalseTotal
if (len >= r->size && 836 221 1057
106 836 ▶ strncasecmp(path, r->ptr, r->size) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
strncasecmp(path, r->ptr, r->size) == 0) { 13 823 836
107 13 start = r->size;
108 13 break;
109 }
110 }
111  
112 535 ▶ if (!start)
Branch: 1 branch
BranchTrueFalseTotal
if (!start) 522 13 535
113 522 return true;
114  
115 /*
116 * Reject paths that start with Windows-style directory separators
117 * (".git\") or NTFS alternate streams (".git:") and could be used
118 * to write to the ".git" directory on Windows platforms.
119 */
120 13 ▶ if (path[start] == '\\' || path[start] == ':')
Branch: 2 branches
BranchTrueFalseTotal
if (path[start] == '\\' || path[start] == ':') 13 0 13
if (path[start] == '\\' || path[start] == ':') 0 13 13
121 0 return false;
122  
123 /* Reject paths like '.git ' or '.git.' */
124 13 ▶ for (i = start; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = start; i < len; i++) { 13 0 13
125 13 ▶ if (path[i] != ' ' && path[i] != '.')
Branch: 2 branches
BranchTrueFalseTotal
if (path[i] != ' ' && path[i] != '.') 13 0 13
if (path[i] != ' ' && path[i] != '.') 13 0 13
126 13 return true;
127 }
128  
129 0 return false;
130 }
131  
132 /*
133 * Windows paths that end with spaces and/or dots are elided to the
134 * path without them for backward compatibility. That is to say
135 * that opening file "foo ", "foo." or even "foo . . ." will all
136 * map to a filename of "foo". This function identifies spaces and
137 * dots at the end of a filename, whether the proper end of the
138 * filename (end of string) or a colon (which would indicate a
139 * Windows alternate data stream.)
140 */
141 0 GIT_INLINE(bool) ntfs_end_of_filename(const char *path)
142 {
143 0 const char *c = path;
144  
145 0 for (;; c++) {
146 0 ▶ if (*c == '\0' || *c == ':')
Branch: 2 branches
BranchTrueFalseTotal
if (*c == '\0' || *c == ':') 0 0 0
if (*c == '\0' || *c == ':') 0 0 0
147 0 return true;
148 0 ▶ if (*c != ' ' && *c != '.')
Branch: 2 branches
BranchTrueFalseTotal
if (*c != ' ' && *c != '.') 0 0 0
if (*c != ' ' && *c != '.') 0 0 0
149 0 return false;
150 }
151  
152 return true;
153 }
154  
155 0 GIT_INLINE(bool) validate_dotgit_ntfs_generic(
156 const char *name,
157 size_t len,
158 const char *dotgit_name,
159 size_t dotgit_len,
160 const char *shortname_pfix)
161 {
162 int i, saw_tilde;
163  
164 0 ▶ if (name[0] == '.' && len >= dotgit_len &&
Branch: 2 branches
BranchTrueFalseTotal
if (name[0] == '.' && len >= dotgit_len && 0 0 0
if (name[0] == '.' && len >= dotgit_len && 0 0 0
165 0 ▶ !strncasecmp(name + 1, dotgit_name, dotgit_len)) {
Branch: 1 branch
BranchTrueFalseTotal
!strncasecmp(name + 1, dotgit_name, dotgit_len)) { 0 0 0
166 0 return !ntfs_end_of_filename(name + dotgit_len + 1);
167 }
168  
169 /* Detect the basic NTFS shortname with the first six chars */
170 0 ▶ if (!strncasecmp(name, dotgit_name, 6) && name[6] == '~' &&
Branch: 2 branches
BranchTrueFalseTotal
if (!strncasecmp(name, dotgit_name, 6) && name[6] == '~' && 0 0 0
if (!strncasecmp(name, dotgit_name, 6) && name[6] == '~' && 0 0 0
171 0 ▶ name[7] >= '1' && name[7] <= '4')
Branch: 2 branches
BranchTrueFalseTotal
name[7] >= '1' && name[7] <= '4') 0 0 0
name[7] >= '1' && name[7] <= '4') 0 0 0
172 0 return !ntfs_end_of_filename(name + 8);
173  
174 /* Catch fallback names */
175 0 ▶ for (i = 0, saw_tilde = 0; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0, saw_tilde = 0; i < 8; i++) { 0 0 0
176 0 ▶ if (name[i] == '\0') {
Branch: 1 branch
BranchTrueFalseTotal
if (name[i] == '\0') { 0 0 0
177 0 return true;
178 0 ▶ } else if (saw_tilde) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (saw_tilde) { 0 0 0
179 0 ▶ if (name[i] < '0' || name[i] > '9')
Branch: 2 branches
BranchTrueFalseTotal
if (name[i] < '0' || name[i] > '9') 0 0 0
if (name[i] < '0' || name[i] > '9') 0 0 0
180 0 return true;
181 0 ▶ } else if (name[i] == '~') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (name[i] == '~') { 0 0 0
182 0 ▶ if (name[i+1] < '1' || name[i+1] > '9')
Branch: 2 branches
BranchTrueFalseTotal
if (name[i+1] < '1' || name[i+1] > '9') 0 0 0
if (name[i+1] < '1' || name[i+1] > '9') 0 0 0
183 0 return true;
184 0 saw_tilde = 1;
185 0 ▶ } else if (i >= 6) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (i >= 6) { 0 0 0
186 0 return true;
187 0 ▶ } else if ((unsigned char)name[i] > 127) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ((unsigned char)name[i] > 127) { 0 0 0
188 0 return true;
189 0 ▶ } else if (git__tolower(name[i]) != shortname_pfix[i]) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (git__tolower(name[i]) != shortname_pfix[i]) { 0 0 0
190 0 return true;
191 }
192 }
193  
194 0 return !ntfs_end_of_filename(name + i);
195 }
196  
197 /*
198 * Return the length of the common prefix between str and prefix, comparing them
199 * case-insensitively (must be ASCII to match).
200 */
201 0 GIT_INLINE(size_t) common_prefix_icase(const char *str, size_t len, const char *prefix)
202 {
203 0 size_t count = 0;
204  
205 0 ▶ while (len > 0 && tolower(*str) == tolower(*prefix)) {
Branch: 2 branches
BranchTrueFalseTotal
while (len > 0 && tolower(*str) == tolower(*prefix)) { 0 0 0
while (len > 0 && tolower(*str) == tolower(*prefix)) { 0 0 0
206 0 count++;
207 0 str++;
208 0 prefix++;
209 0 len--;
210 }
211  
212 0 return count;
213 }
214  
215 803 static bool validate_repo_component(
216 const char *component,
217 size_t len,
218 void *payload)
219 {
220 803 repository_path_validate_data *data = (repository_path_validate_data *)payload;
221  
222 803 ▶ if (data->flags & GIT_PATH_REJECT_DOT_GIT_HFS) {
Branch: 1 branch
BranchTrueFalseTotal
if (data->flags & GIT_PATH_REJECT_DOT_GIT_HFS) { 0 803 803
223 0 ▶ if (!validate_dotgit_hfs(component, len))
Branch: 1 branch
BranchTrueFalseTotal
if (!validate_dotgit_hfs(component, len)) 0 0 0
224 0 return false;
225  
226 0 if (S_ISLNK(data->file_mode) &&
227 0 git_path_is_gitfile(component, len, GIT_PATH_GITFILE_GITMODULES, GIT_PATH_FS_HFS))
228 0 return false;
229 }
230  
231 803 ▶ if (data->flags & GIT_PATH_REJECT_DOT_GIT_NTFS) {
Branch: 1 branch
BranchTrueFalseTotal
if (data->flags & GIT_PATH_REJECT_DOT_GIT_NTFS) { 535 268 803
232 535 ▶ if (!validate_dotgit_ntfs(data->repo, component, len))
Branch: 1 branch
BranchTrueFalseTotal
if (!validate_dotgit_ntfs(data->repo, component, len)) 0 535 535
233 0 return false;
234  
235 535 if (S_ISLNK(data->file_mode) &&
236 0 git_path_is_gitfile(component, len, GIT_PATH_GITFILE_GITMODULES, GIT_PATH_FS_NTFS))
237 0 return false;
238 }
239  
240 /* don't bother rerunning the `.git` test if we ran the HFS or NTFS
241 * specific tests, they would have already rejected `.git`.
242 */
243 803 ▶ if ((data->flags & GIT_PATH_REJECT_DOT_GIT_HFS) == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
if ((data->flags & GIT_PATH_REJECT_DOT_GIT_HFS) == 0 && 803 0 803
244 803 ▶ (data->flags & GIT_PATH_REJECT_DOT_GIT_NTFS) == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
(data->flags & GIT_PATH_REJECT_DOT_GIT_NTFS) == 0 && 268 535 803
245 268 ▶ (data->flags & GIT_PATH_REJECT_DOT_GIT_LITERAL)) {
Branch: 1 branch
BranchTrueFalseTotal
(data->flags & GIT_PATH_REJECT_DOT_GIT_LITERAL)) { 0 268 268
246 0 ▶ if (len >= 4 &&
Branch: 1 branch
BranchTrueFalseTotal
if (len >= 4 && 0 0 0
247 0 ▶ component[0] == '.' &&
Branch: 1 branch
BranchTrueFalseTotal
component[0] == '.' && 0 0 0
248 0 ▶ (component[1] == 'g' || component[1] == 'G') &&
Branch: 2 branches
BranchTrueFalseTotal
(component[1] == 'g' || component[1] == 'G') && 0 0 0
(component[1] == 'g' || component[1] == 'G') && 0 0 0
249 0 ▶ (component[2] == 'i' || component[2] == 'I') &&
Branch: 2 branches
BranchTrueFalseTotal
(component[2] == 'i' || component[2] == 'I') && 0 0 0
(component[2] == 'i' || component[2] == 'I') && 0 0 0
250 0 ▶ (component[3] == 't' || component[3] == 'T')) {
Branch: 2 branches
BranchTrueFalseTotal
(component[3] == 't' || component[3] == 'T')) { 0 0 0
(component[3] == 't' || component[3] == 'T')) { 0 0 0
251 0 ▶ if (len == 4)
Branch: 1 branch
BranchTrueFalseTotal
if (len == 4) 0 0 0
252 0 return false;
253  
254 0 if (S_ISLNK(data->file_mode) &&
255 0 common_prefix_icase(component, len, ".gitmodules") == len)
256 0 return false;
257 }
258 }
259  
260 803 return true;
261 }
262  
263 463 GIT_INLINE(unsigned int) dotgit_flags(
264 git_repository *repo,
265 unsigned int flags)
266 {
267 463 int protectHFS = 0, protectNTFS = 1;
268 463 int error = 0;
269  
270 463 flags |= GIT_PATH_REJECT_DOT_GIT_LITERAL;
271  
272 #ifdef __APPLE__
273 protectHFS = 1;
274 #endif
275  
276 463 ▶ if (repo && !protectHFS)
Branch: 2 branches
BranchTrueFalseTotal
if (repo && !protectHFS) 351 112 463
if (repo && !protectHFS) 351 0 351
277 351 error = git_repository__configmap_lookup(&protectHFS, repo, GIT_CONFIGMAP_PROTECTHFS);
278 463 ▶ if (!error && protectHFS)
Branch: 2 branches
BranchTrueFalseTotal
if (!error && protectHFS) 463 0 463
if (!error && protectHFS) 0 463 463
279 0 flags |= GIT_PATH_REJECT_DOT_GIT_HFS;
280  
281 463 ▶ if (repo)
Branch: 1 branch
BranchTrueFalseTotal
if (repo) 351 112 463
282 351 error = git_repository__configmap_lookup(&protectNTFS, repo, GIT_CONFIGMAP_PROTECTNTFS);
283 463 ▶ if (!error && protectNTFS)
Branch: 2 branches
BranchTrueFalseTotal
if (!error && protectNTFS) 463 0 463
if (!error && protectNTFS) 463 0 463
284 463 flags |= GIT_PATH_REJECT_DOT_GIT_NTFS;
285  
286 463 return flags;
287 }
288  
289 6802 GIT_INLINE(unsigned int) length_flags(
290 git_repository *repo,
291 unsigned int flags)
292 {
293 #ifdef GIT_WIN32
294 int allow = 0;
295  
296 if (repo &&
297 git_repository__configmap_lookup(&allow, repo, GIT_CONFIGMAP_LONGPATHS) < 0)
298 allow = 0;
299  
300 if (allow)
301 flags &= ~GIT_FS_PATH_REJECT_LONG_PATHS;
302  
303 #else
304 6802 GIT_UNUSED(repo);
305 6802 flags &= ~GIT_FS_PATH_REJECT_LONG_PATHS;
306 #endif
307  
308 6802 return flags;
309 }
310  
311 7380 bool git_path_str_is_valid(
312 git_repository *repo,
313 const git_str *path,
314 uint16_t file_mode,
315 unsigned int flags)
316 {
317 7380 repository_path_validate_data data = {0};
318  
319 /* Upgrade the ".git" checks based on platform */
320 7380 ▶ if ((flags & GIT_PATH_REJECT_DOT_GIT))
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & GIT_PATH_REJECT_DOT_GIT)) 463 6917 7380
321 463 flags = dotgit_flags(repo, flags);
322  
323 /* Update the length checks based on platform */
324 7380 ▶ if ((flags & GIT_FS_PATH_REJECT_LONG_PATHS))
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & GIT_FS_PATH_REJECT_LONG_PATHS)) 6802 578 7380
325 6802 flags = length_flags(repo, flags);
326  
327 7380 data.repo = repo;
328 7380 data.file_mode = file_mode;
329 7380 data.flags = flags;
330  
331 7380 return git_fs_path_str_is_valid_ext(path, flags, NULL, validate_repo_component, NULL, &data);
332 }
333  
334 static const struct {
335 const char *file;
336 const char *hash;
337 size_t filelen;
338 } gitfiles[] = {
339 { "gitignore", "gi250a", CONST_STRLEN("gitignore") },
340 { "gitmodules", "gi7eba", CONST_STRLEN("gitmodules") },
341 { "gitattributes", "gi7d29", CONST_STRLEN("gitattributes") }
342 };
343  
344 0 extern int git_path_is_gitfile(
345 const char *path,
346 size_t pathlen,
347 git_path_gitfile gitfile,
348 git_path_fs fs)
349 {
350 const char *file, *hash;
351 size_t filelen;
352  
353 0 ▶ if (!(gitfile >= GIT_PATH_GITFILE_GITIGNORE && gitfile < ARRAY_SIZE(gitfiles))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(gitfile >= GIT_PATH_GITFILE_GITIGNORE && gitfile < ARRAY_SIZE(gitfiles))) { 0 0 0
354 0 git_error_set(GIT_ERROR_OS, "invalid gitfile for path validation");
355 0 return -1;
356 }
357  
358 0 file = gitfiles[gitfile].file;
359 0 filelen = gitfiles[gitfile].filelen;
360 0 hash = gitfiles[gitfile].hash;
361  
362 0 switch (fs) {
363 0 case GIT_PATH_FS_GENERIC:
364 0 ▶ return !validate_dotgit_ntfs_generic(path, pathlen, file, filelen, hash) ||
Branch: 1 branch
BranchTrueFalseTotal
return !validate_dotgit_ntfs_generic(path, pathlen, file, filelen, hash) || 0 0 0
365 0 ▶ !validate_dotgit_hfs_generic(path, pathlen, file, filelen);
Branch: 1 branch
BranchTrueFalseTotal
!validate_dotgit_hfs_generic(path, pathlen, file, filelen); 0 0 0
366 0 case GIT_PATH_FS_NTFS:
367 0 return !validate_dotgit_ntfs_generic(path, pathlen, file, filelen, hash);
368 0 case GIT_PATH_FS_HFS:
369 0 return !validate_dotgit_hfs_generic(path, pathlen, file, filelen);
370 0 default:
371 0 git_error_set(GIT_ERROR_OS, "invalid filesystem for path validation");
372 0 return -1;
373 }
374 }
375  
« deps/libgit2/src/libgit2/xdiff/xdiffi.c ↑ Summary deps/libgit2/src/libgit2/branch.c »