diff options
author | Bartosz Sosnowski <bartosz@janeasystems.com> | 2017-03-14 18:22:53 +0100 |
---|---|---|
committer | Myles Borins <mylesborins@google.com> | 2017-04-18 20:08:39 -0400 |
commit | 8ed18a1429e62d19bffc04b696a517e18c7475b2 (patch) | |
tree | 6b26a9d7cbee0537ecd04507eb601e3c3bc00e27 | |
parent | ab3fdf531fe0536ae6b3a657d7fb73adc0881a99 (diff) | |
download | node-new-8ed18a1429e62d19bffc04b696a517e18c7475b2.tar.gz |
src: ensure that fd 0-2 are valid on windows
Check that stdin, stdout and stderr are valid file descriptors on
Windows. If not, reopen them with 'nul' file.
Refs: https://github.com/nodejs/node/pull/875
Fixes: https://github.com/nodejs/node/issues/11656
PR-URL: https://github.com/nodejs/node/pull/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>
-rw-r--r-- | src/node.cc | 13 | ||||
-rw-r--r-- | test/fixtures/spawn_closed_stdio.py | 8 | ||||
-rw-r--r-- | test/parallel/test-stdio-closed.js | 14 |
3 files changed, 34 insertions, 1 deletions
diff --git a/src/node.cc b/src/node.cc index 63c49c49b0..b48c4b06a1 100644 --- a/src/node.cc +++ b/src/node.cc @@ -3995,6 +3995,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 } diff --git a/test/fixtures/spawn_closed_stdio.py b/test/fixtures/spawn_closed_stdio.py new file mode 100644 index 0000000000..b5de2552c2 --- /dev/null +++ b/test/fixtures/spawn_closed_stdio.py @@ -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) diff --git a/test/parallel/test-stdio-closed.js b/test/parallel/test-stdio-closed.js index 98e4f980d5..2313140a26 100644 --- a/test/parallel/test-stdio-closed.js +++ b/test/parallel/test-stdio-closed.js @@ -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; } |