Skip to content

Commit

Permalink
src: ensure that fd 0-2 are valid on windows
Browse files Browse the repository at this point in the history
Check that stdin, stdout and stderr are valid file descriptors on
Windows. If not, reopen them with 'nul' file.

Refs: nodejs#875
Fixes: nodejs#11656
PR-URL: nodejs#11863
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
bzoz authored and jungx098 committed Mar 21, 2017
1 parent e8d9da8 commit 68c06b6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4213,6 +4213,19 @@ inline void PlatformInit() {
} while (min + 1 < max);
}
#endif // __POSIX__
#ifdef _WIN32
for (int fd = 0; fd <= 2; ++fd) {
auto handle = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
if (handle == INVALID_HANDLE_VALUE ||
GetFileType(handle) == FILE_TYPE_UNKNOWN) {
// Ignore _close result. If it fails or not depends on used Windows
// version. We will just check _open result.
_close(fd);
if (fd != _open("nul", _O_RDWR))
ABORT();
}
}
#endif // _WIN32
}


Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/spawn_closed_stdio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os
import sys
import subprocess
os.close(0)
os.close(1)
os.close(2)
exit_code = subprocess.call(sys.argv[1:], shell=False)
sys.exit(exit_code)
14 changes: 13 additions & 1 deletion test/parallel/test-stdio-closed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,21 @@ const common = require('../common');
const assert = require('assert');
const spawn = require('child_process').spawn;
const fs = require('fs');
const path = require('path');

if (common.isWindows) {
common.skip('platform not supported.');
if (process.argv[2] === 'child') {
process.stdin;
process.stdout;
process.stderr;
return;
}
const python = process.env.PYTHON || 'python';
const script = path.join(common.fixturesDir, 'spawn_closed_stdio.py');
const proc = spawn(python, [script, process.execPath, __filename, 'child']);
proc.on('exit', common.mustCall(function(exitCode) {
assert.strictEqual(exitCode, 0);
}));
return;
}

Expand Down

0 comments on commit 68c06b6

Please sign in to comment.