@matheusbsbs / matheus-bagliano / commits / 98d9b23e90
Fork 0

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 <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Dec 21, 2019 at 22:05 UTC 98d9b23e90510c5acafa2ebc9463cc2293f40df0
1 file changed +16 -10
compat/mingw.c
+16 -10
@@ -484,16 +484,16 @@ int mingw_open (const char *filename, int oflags, ...)
484 return -1;
485 }
486
487 - if (filename && !strcmp(filename, "/dev/null"))
488 - filename = "nul";
489 -
487 if ((oflags & O_APPEND) && !is_local_named_pipe_path(filename))
488 open_fn = mingw_open_append;
489 else
490 open_fn = _wopen;
491
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)
495 return -1;
496 +
497 fd = open_fn(wfilename, oflags, mode);
498
499 if (fd < 0 && (oflags & O_ACCMODE) != O_RDONLY && errno == EACCES) {
@@ -556,10 +556,13 @@ FILE *mingw_fopen (const char *filename, const char *otype)
556 return NULL;
557 }
558 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)
561 return NULL;
562 +
563 + if (xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
564 + return NULL;
565 +
566 if (hide && !access(filename, F_OK) && set_hidden_flag(wfilename, 0)) {
567 error("could not unhide %s", filename);
568 return NULL;
@@ -583,10 +586,13 @@ FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
586 return NULL;
587 }
588 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)
591 return NULL;
592 +
593 + if (xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
594 + return NULL;
595 +
596 if (hide && !access(filename, F_OK) && set_hidden_flag(wfilename, 0)) {
597 error("could not unhide %s", filename);
598 return NULL;