Skip to content

Commit 014c668

Browse files
[windows] file_write: require a console, not just a character device
_isatty is true for any character device, so a write to NUL took the WriteConsoleW path. WriteConsoleW fails on a non-console handle and the file_error after it throws from inside a GC-free zone, so the process dies instead of reporting anything: sys.io.File.write("NUL").writeString("x") prog.exe >NUL GetConsoleMode only succeeds on a real console, and __hxcpp_print already uses it. _isatty stays as the first check so files and pipes take the same fwrite path they did before. Came in with 1544ff5 (HaxeFoundation#1307), which fixed utf console output. That part is unchanged here.
1 parent 0c59364 commit 014c668

5 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎Changes.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* Updated sqlite to 3.40.1
1717
* Updated zlib to 1.2.13
1818

19+
* Fixed crash writing to the null device on Windows
1920
* Fixed SSL socket non blocking handshake throwing an exception on 64bit Windows
2021
* Fixed Windows 64bit architecture detection
2122
* Fixed critial error handler returning the wrong callstack

‎src/hx/libs/std/File.cpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ int _hx_std_file_write( Dynamic handle, Array<unsigned char> s, int p, int n )
149149

150150
hx::AutoGCFreeZone zone;
151151
#ifdef HX_WINDOWS
152-
if (_isatty(_fileno(f->io))) {
152+
// _isatty is true for ANY character device - NUL, a serial port, a printer - and none of
153+
// those accept WriteConsoleW. Only a real console has a console mode.
154+
DWORD console_mode;
155+
if (_isatty(_fileno(f->io)) && GetConsoleMode((HANDLE)_get_osfhandle(_fileno(f->io)), &console_mode)) {
153156
fflush(f->io);
154157
HANDLE win_handle = (HANDLE)_get_osfhandle(_fileno(f->io));
155158
static const int MAX_BUFFER_SIZE = 8192;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// The null device is a character device, so _isatty reports it as a tty. file_write used that as
2+
// its test for a console and called WriteConsoleW, which only accepts a console handle; the failure
3+
// threw from inside a GC-free zone and took the process down instead of reporting an error.
4+
function main() {
5+
final nul = Sys.systemName() == "Windows" ? "NUL" : "/dev/null";
6+
7+
final out = sys.io.File.write(nul);
8+
out.writeString("written to the null device\n");
9+
out.flush();
10+
out.close();
11+
12+
Sys.println("ok");
13+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-cpp bin
2+
-m Main
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ok

0 commit comments

Comments
 (0)