Browse Source

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>
v6
Bartosz Sosnowski 8 years ago
committed by Anna Henningsen
parent
commit
bd496e0187
No known key found for this signature in database GPG Key ID: D8B9F5AEAE84E4CF
  1. 13
      src/node.cc
  2. 8
      test/fixtures/spawn_closed_stdio.py
  3. 14
      test/parallel/test-stdio-closed.js

13
src/node.cc

@ -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
}

8
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)

14
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;
}

Loading…
Cancel
Save