Skip to content

Commit 033c2dc

Browse files
committed
Merge branch 'ef/maint-win-verify-path'
* ef/maint-win-verify-path: verify_dotfile(): do not assume '/' is the path seperator verify_path(): simplify check at the directory boundary verify_path: consider dos drive prefix real_path: do not assume '/' is the path seperator A Windows path starting with a backslash is absolute
2 parents 1fd7ef2 + e0f530f commit 033c2dc

File tree

5 files changed

+27
-17
lines changed

5 files changed

+27
-17
lines changed

abspath.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const char *real_path(const char *path)
4040

4141
while (depth--) {
4242
if (!is_directory(buf)) {
43-
char *last_slash = strrchr(buf, '/');
43+
char *last_slash = find_last_dir_sep(buf);
4444
if (last_slash) {
4545
*last_slash = '\0';
4646
last_elem = xstrdup(last_slash + 1);
@@ -65,7 +65,7 @@ const char *real_path(const char *path)
6565
if (len + strlen(last_elem) + 2 > PATH_MAX)
6666
die ("Too long path name: '%s/%s'",
6767
buf, last_elem);
68-
if (len && buf[len-1] != '/')
68+
if (len && !is_dir_sep(buf[len-1]))
6969
buf[len++] = '/';
7070
strcpy(buf + len, last_elem);
7171
free(last_elem);

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ extern char *expand_user_path(const char *path);
747747
char *enter_repo(char *path, int strict);
748748
static inline int is_absolute_path(const char *path)
749749
{
750-
return path[0] == '/' || has_dos_drive_prefix(path);
750+
return is_dir_sep(path[0]) || has_dos_drive_prefix(path);
751751
}
752752
int is_directory(const char *);
753753
const char *real_path(const char *path);

compat/mingw.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,15 @@ int winansi_fprintf(FILE *stream, const char *format, ...) __attribute__((format
300300

301301
#define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':')
302302
#define is_dir_sep(c) ((c) == '/' || (c) == '\\')
303+
static inline char *mingw_find_last_dir_sep(const char *path)
304+
{
305+
char *ret = NULL;
306+
for (; *path; ++path)
307+
if (is_dir_sep(*path))
308+
ret = (char *)path;
309+
return ret;
310+
}
311+
#define find_last_dir_sep mingw_find_last_dir_sep
303312
#define PATH_SEP ';'
304313
#define PRIuMAX "I64u"
305314

git-compat-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ extern char *gitbasename(char *);
215215
#define is_dir_sep(c) ((c) == '/')
216216
#endif
217217

218+
#ifndef find_last_dir_sep
219+
#define find_last_dir_sep(path) strrchr(path, '/')
220+
#endif
221+
218222
#if __HP_cc >= 61000
219223
#define NORETURN __attribute__((noreturn))
220224
#define NORETURN_PTR

read-cache.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -726,11 +726,12 @@ static int verify_dotfile(const char *rest)
726726
* has already been discarded, we now test
727727
* the rest.
728728
*/
729-
switch (*rest) {
729+
730730
/* "." is not allowed */
731-
case '\0': case '/':
731+
if (*rest == '\0' || is_dir_sep(*rest))
732732
return 0;
733733

734+
switch (*rest) {
734735
/*
735736
* ".git" followed by NUL or slash is bad. This
736737
* shares the path end test with the ".." case.
@@ -743,7 +744,7 @@ static int verify_dotfile(const char *rest)
743744
rest += 2;
744745
/* fallthrough */
745746
case '.':
746-
if (rest[1] == '\0' || rest[1] == '/')
747+
if (rest[1] == '\0' || is_dir_sep(rest[1]))
747748
return 0;
748749
}
749750
return 1;
@@ -753,23 +754,19 @@ int verify_path(const char *path)
753754
{
754755
char c;
755756

757+
if (has_dos_drive_prefix(path))
758+
return 0;
759+
756760
goto inside;
757761
for (;;) {
758762
if (!c)
759763
return 1;
760-
if (c == '/') {
764+
if (is_dir_sep(c)) {
761765
inside:
762766
c = *path++;
763-
switch (c) {
764-
default:
765-
continue;
766-
case '/': case '\0':
767-
break;
768-
case '.':
769-
if (verify_dotfile(path))
770-
continue;
771-
}
772-
return 0;
767+
if ((c == '.' && !verify_dotfile(path)) ||
768+
is_dir_sep(c) || c == '\0')
769+
return 0;
773770
}
774771
c = *path++;
775772
}

0 commit comments

Comments
 (0)