Skip to content

Commit e6806a6

Browse files
committed
Refactor filename check to be wrapped in macro that no-ops outside of Windows
Signed-off-by: Lincoln Atkinson <[email protected]>
1 parent c4a42f2 commit e6806a6

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

compat/mingw.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,6 +2444,21 @@ int mingw_skip_dos_drive_prefix(char **path)
24442444
return ret;
24452445
}
24462446

2447+
int mingw_known_invalid(const char *path)
2448+
{
2449+
// Colon is admissible as part of absolute path (e.g. "C:\file.txt")
2450+
// but otherwise invalid. Explicit checking done to prevent
2451+
// unintentional writing to alternate data stream path, e.g.
2452+
// "some\path\file:streamname"
2453+
if (path) {
2454+
path += has_dos_drive_prefix(path);
2455+
for (; *path; ++path)
2456+
if (*path == ':')
2457+
return 1;
2458+
}
2459+
return 0;
2460+
}
2461+
24472462
int mingw_offset_1st_component(const char *path)
24482463
{
24492464
char *pos = (char *)path;

compat/mingw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ HANDLE winansi_get_osfhandle(int fd);
423423
(isalpha(*(path)) && (path)[1] == ':' ? 2 : 0)
424424
int mingw_skip_dos_drive_prefix(char **path);
425425
#define skip_dos_drive_prefix mingw_skip_dos_drive_prefix
426+
int mingw_known_invalid(const char *path);
427+
#define known_invalid mingw_known_invalid
426428
#define has_unc_prefix(path) (*(path) == '\\' && (path)[1] == '\\' ? 2 : 0)
427429
#define is_dir_sep(c) ((c) == '/' || (c) == '\\')
428430
static inline char *mingw_find_last_dir_sep(const char *path)

git-compat-util.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,14 @@ static inline int git_skip_dos_drive_prefix(char **path)
347347
#define skip_dos_drive_prefix git_skip_dos_drive_prefix
348348
#endif
349349

350+
#ifndef known_invalid
351+
static inline int git_known_invalid(const char *path)
352+
{
353+
return 0;
354+
}
355+
#define known_invalid git_known_invalid
356+
#endif
357+
350358
#ifndef has_unc_prefix
351359
static inline int git_has_unc_prefix(const char *path)
352360
{

read-cache.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,9 @@ int verify_path(const char *path)
809809
if (has_dos_drive_prefix(path))
810810
return 0;
811811

812+
if (known_invalid(path))
813+
return 0;
814+
812815
goto inside;
813816
for (;;) {
814817
if (!c)
@@ -821,10 +824,9 @@ int verify_path(const char *path)
821824
return 0;
822825
c = *path++;
823826
if ((c == '.' && !verify_dotfile(path)) ||
824-
is_dir_sep(c) || c == ':' || c == '\0')
827+
is_dir_sep(c) || c == '\0')
825828
return 0;
826-
} else if (c == ':')
827-
return 0;
829+
}
828830
c = *path++;
829831
}
830832
}

0 commit comments

Comments
 (0)