The attached program prints the stat() mode of the file name "NUL".
Result with MSVC 14, mingw 13 "UCRT":
$ ./foo
ret = 0, mode = 0x2000, regular? = 0, character? = 1
fd = 3
Result with mingw 5, 10, 13:
$ ./foo
ret = 0, mode = 0x81b6, regular? = 1, character? = 0
fd = 3
The latter result makes no sense: "NUL" is not a regular file,
it's a character device.
This is a bug in the msvcrt
_statfunction. mingw-w64 providesstatfunction which is wrapper around the msvcrt_stat(or UCRT_statdepending on the configuration) and already fixes some msvcrt issues (e.g. allow passing directory name with trailing slash). But this issue one with "NUL" filename is not handled.On the other hand, msvcrt
_fstatfunction on fd retrieved fromopen("NUL", O_RDWR)is working correctly.Seems that this is a problem also directly in the WinAPI
FindFirstFilefunction, which_statis probably using.So to fix this mingw-w64
statfunction bug (not the msvcrt_statfunction), it would be needed to implement mingw-w64statas wrapper around msvcrt_fstatfunction. But for that case the msvcrt_opencannot be used as it cannot open directory. Instead of_openit would be needed combination of WinAPICreateFileAwithFILE_FLAG_BACKUP_SEMANTICSflag (which is for allowing to open handle to directory) and msvcrt_open_osfhandlefunction which get CRT fd from WinAPI HANDLE.I implement the fix as described above and sent it to the list:
https://sourceforge.net/p/mingw-w64/mailman/mingw-w64-public/thread/20260224201812.4f7jrjgbbx5ovjwm%40pali/
With the change, the mingw-w64
stat()function forNULpath when build against msvcrt.dll will properly returncharacter? = 1viaS_ISCHR().This has been fixed in 6d91c946651a384d19b7d8fc69ba8d3df3ab3e4b.