test: introduce common.runWithInvalidFD()

This provides a more reliable way to get a fd that can be used
to tirgger EBADF.

PR-URL: https://github.com/nodejs/node/pull/18864
Fixes: https://github.com/nodejs/node/issues/18820
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Joyee Cheung 2018-02-19 17:36:59 +08:00
parent 13637d23f7
commit acf2fd39f7
No known key found for this signature in database
GPG Key ID: F586868AAD831D0C
2 changed files with 21 additions and 0 deletions

View File

@ -139,6 +139,14 @@ consisting of all `ArrayBufferView` and an `ArrayBuffer`.
Returns the file name and line number for the provided Function.
### runWithInvalidFD(func)
* `func` [&lt;Function>]
Runs `func` with an invalid file descriptor that is an unsigned integer and
can be used to trigger `EBADF` as the first argument. If no such file
descriptor could be generated, a skip message will be printed and the `func`
will not be run.
### globalCheck
* [&lt;boolean>]

View File

@ -816,6 +816,19 @@ function restoreWritable(name) {
delete process[name].writeTimes;
}
exports.runWithInvalidFD = function(func) {
let fd = 1 << 30;
// Get first known bad file descriptor. 1 << 30 is usually unlikely to
// be an valid one.
try {
while (fs.fstatSync(fd--) && fd > 0);
} catch (e) {
return func(fd);
}
exports.printSkipMessage('Could not generate an invalid fd');
};
exports.hijackStdout = hijackStdWritable.bind(null, 'stdout');
exports.hijackStderr = hijackStdWritable.bind(null, 'stderr');
exports.restoreStdout = restoreWritable.bind(null, 'stdout');