Skip to content

Commit acf2fd3

Browse files
committed
test: introduce common.runWithInvalidFD()
This provides a more reliable way to get a fd that can be used to tirgger EBADF. PR-URL: #18864 Fixes: #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>
1 parent 13637d2 commit acf2fd3

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

test/common/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ consisting of all `ArrayBufferView` and an `ArrayBuffer`.
139139

140140
Returns the file name and line number for the provided Function.
141141

142+
### runWithInvalidFD(func)
143+
* `func` [&lt;Function>]
144+
145+
Runs `func` with an invalid file descriptor that is an unsigned integer and
146+
can be used to trigger `EBADF` as the first argument. If no such file
147+
descriptor could be generated, a skip message will be printed and the `func`
148+
will not be run.
149+
142150
### globalCheck
143151
* [&lt;boolean>]
144152

test/common/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,19 @@ function restoreWritable(name) {
816816
delete process[name].writeTimes;
817817
}
818818

819+
exports.runWithInvalidFD = function(func) {
820+
let fd = 1 << 30;
821+
// Get first known bad file descriptor. 1 << 30 is usually unlikely to
822+
// be an valid one.
823+
try {
824+
while (fs.fstatSync(fd--) && fd > 0);
825+
} catch (e) {
826+
return func(fd);
827+
}
828+
829+
exports.printSkipMessage('Could not generate an invalid fd');
830+
};
831+
819832
exports.hijackStdout = hijackStdWritable.bind(null, 'stdout');
820833
exports.hijackStderr = hijackStdWritable.bind(null, 'stderr');
821834
exports.restoreStdout = restoreWritable.bind(null, 'stdout');

0 commit comments

Comments
 (0)