Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: ensure that fd 0-2 are valid on windows #11863

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4213,6 +4213,16 @@ inline void PlatformInit() {
} while (min + 1 < max);
}
#endif // __POSIX__
#ifdef _WIN32
for (int fd = 0; fd <= 2; ++fd) {
auto handle = (HANDLE) _get_osfhandle(fd);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use the appropriate C++-style cast here, whichever one that is?

if (handle == INVALID_HANDLE_VALUE ||
GetFileType(handle) == FILE_TYPE_UNKNOWN) {
if (_close(fd) || fd != _open("nul", _O_RDWR))
ABORT();
}
}
#endif // _WIN32
}


Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/spawn_closed_stdio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import os
import sys
import subprocess
os.close(0)
os.close(1)
os.close(2)
cmd = sys.argv[1] + ' -e "process.stdin; process.stdout; process.stderr;"'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would prefer it if the arguments came from the test file, i.e. you just spawn sys.argv[1] here and move the -e … bits to test-stdio-closed.js

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do use -e, write this as cmd = sys.argv[1:2] + ['-e', 'process.stdin; ...'] so it gets escaped properly.

exit_code = subprocess.call(cmd, shell=False)
sys.exit(exit_code)
9 changes: 8 additions & 1 deletion test/parallel/test-stdio-closed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ 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.');
const python = process.env.PYTHON || 'python';
const script = path.join(__dirname, '..', 'fixtures',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path.join(common.fixturesDir, 'spawn_closed_stdio.py')?

'spawn_closed_stdio.py');
const proc = spawn(python, [ script, process.execPath ]);
proc.on('exit', common.mustCall(function(exitCode) {
assert.strictEqual(exitCode, 0);
}));
return;
}

Expand Down