Skip to content

Commit 98d9b23

Browse files
dschogitster
authored andcommitted
mingw: short-circuit the conversion of /dev/null to UTF-16
In the next commit, we want to disallow accessing any path that contains any segment that is equivalent to `NUL`. In particular, we want to disallow accessing `NUL` (e.g. to prevent any repository from being checked out that contains a file called `NUL`, as that is not a valid file name on Windows). However, there are legitimate use cases within Git itself to write to the Null device. As Git is really a Linux project, it does not abstract that idea, though, but instead uses `/dev/null` to describe this intention. So let's side-step the validation _specifically_ in the case that we want to write to (or read from) `/dev/null`, via a dedicated short-cut in the code that skips the call to `validate_win32_path()`. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 53a06cf commit 98d9b23

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

compat/mingw.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -484,16 +484,16 @@ int mingw_open (const char *filename, int oflags, ...)
484484
return -1;
485485
}
486486

487-
if (filename && !strcmp(filename, "/dev/null"))
488-
filename = "nul";
489-
490487
if ((oflags & O_APPEND) && !is_local_named_pipe_path(filename))
491488
open_fn = mingw_open_append;
492489
else
493490
open_fn = _wopen;
494491

495-
if (xutftowcs_path(wfilename, filename) < 0)
492+
if (filename && !strcmp(filename, "/dev/null"))
493+
wcscpy(wfilename, L"nul");
494+
else if (xutftowcs_path(wfilename, filename) < 0)
496495
return -1;
496+
497497
fd = open_fn(wfilename, oflags, mode);
498498

499499
if (fd < 0 && (oflags & O_ACCMODE) != O_RDONLY && errno == EACCES) {
@@ -556,10 +556,13 @@ FILE *mingw_fopen (const char *filename, const char *otype)
556556
return NULL;
557557
}
558558
if (filename && !strcmp(filename, "/dev/null"))
559-
filename = "nul";
560-
if (xutftowcs_path(wfilename, filename) < 0 ||
561-
xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
559+
wcscpy(wfilename, L"nul");
560+
else if (xutftowcs_path(wfilename, filename) < 0)
562561
return NULL;
562+
563+
if (xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
564+
return NULL;
565+
563566
if (hide && !access(filename, F_OK) && set_hidden_flag(wfilename, 0)) {
564567
error("could not unhide %s", filename);
565568
return NULL;
@@ -583,10 +586,13 @@ FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
583586
return NULL;
584587
}
585588
if (filename && !strcmp(filename, "/dev/null"))
586-
filename = "nul";
587-
if (xutftowcs_path(wfilename, filename) < 0 ||
588-
xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
589+
wcscpy(wfilename, L"nul");
590+
else if (xutftowcs_path(wfilename, filename) < 0)
589591
return NULL;
592+
593+
if (xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
594+
return NULL;
595+
590596
if (hide && !access(filename, F_OK) && set_hidden_flag(wfilename, 0)) {
591597
error("could not unhide %s", filename);
592598
return NULL;

0 commit comments

Comments
 (0)