mingw: do not treat COM0 as a reserved file name

In 4dc42c6c18 (mingw: refuse paths containing reserved names,
2019-12-21), we started disallowing file names that are reserved, e.g.
`NUL`, `CONOUT$`, etc.

This included `COM<n>` where `<n>` is a digit. Unfortunately, this
includes `COM0` but only `COM1`, ..., `COM9` are reserved, according to
the official documentation, `COM0` is mentioned in the "NT Namespaces"
section but it is explicitly _omitted_ from the list of reserved names:
https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions

Tests corroborate this: it is totally possible to write a file called
`com0.c` on Windows 10, but not `com1.c`.

So let's tighten the code to disallow only the reserved `COM<n>` file
names, but to allow `COM0` again.

This fixes https://github.com/git-for-windows/git/issues/2470.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Johannes Schindelin
2020-04-08 18:06:49 +00:00
committed by Junio C Hamano
parent a748f3f3dc
commit b6852e1979
2 changed files with 7 additions and 3 deletions

View File

@ -2590,12 +2590,14 @@ not_a_reserved_name:
continue; continue;
} }
break; break;
case 'c': case 'C': /* COM<N>, CON, CONIN$, CONOUT$ */ case 'c': case 'C':
/* COM1 ... COM9, CON, CONIN$, CONOUT$ */
if ((c = path[++i]) != 'o' && c != 'O') if ((c = path[++i]) != 'o' && c != 'O')
goto not_a_reserved_name; goto not_a_reserved_name;
c = path[++i]; c = path[++i];
if (c == 'm' || c == 'M') { /* COM<N> */ if (c == 'm' || c == 'M') { /* COM1 ... COM9 */
if (!isdigit(path[++i])) c = path[++i];
if (c < '1' || c > '9')
goto not_a_reserved_name; goto not_a_reserved_name;
} else if (c == 'n' || c == 'N') { /* CON */ } else if (c == 'n' || c == 'N') { /* CON */
c = path[i + 1]; c = path[i + 1];

View File

@ -476,6 +476,7 @@ test_expect_success MINGW 'is_valid_path() on Windows' '
C:\\git \ C:\\git \
comm \ comm \
conout.c \ conout.c \
com0.c \
lptN \ lptN \
\ \
--not \ --not \
@ -488,6 +489,7 @@ test_expect_success MINGW 'is_valid_path() on Windows' '
"AUX.c" \ "AUX.c" \
"abc/conOut\$ .xyz/test" \ "abc/conOut\$ .xyz/test" \
lpt8 \ lpt8 \
com9.c \
"lpt*" \ "lpt*" \
Nul \ Nul \
"PRN./abc" "PRN./abc"