node
node copied to clipboard
fs.opendir() and fs.promises.opendir() crash the process on an invalid bufferSize
Version
v28.0.0-pre (main at 95f3cf07), also v22.20.0 and v20.20.2
Platform
Linux 7.0.0-38-generic x86_64
Subsystem
fs
What steps will reproduce the bug?
const fs = require('node:fs');
(async () => {
try {
await fs.promises.opendir(__dirname, { bufferSize: 0 });
} catch (err) {
console.log('caught', err.code);
}
console.log('still running');
})();
The callback form behaves the same:
try {
fs.opendir(__dirname, { bufferSize: 0 }, (err) => console.log('callback', err));
} catch (err) {
console.log('caught', err.code);
}
How often does it reproduce? Is there a required condition?
Always, with any invalid bufferSize (0, -1, 1.5, NaN, '1', null, ...).
What is the expected behavior? Why is that the expected behavior?
The error reaches the caller: fs.opendir() throws (as fs.readdir() and fs.writeFile() do for invalid options), and fs.promises.opendir() rejects. fs.opendirSync() already throws a catchable error.
What do you see instead?
Neither catch runs, the callback is never called, and the process exits with code 1:
RangeError [ERR_OUT_OF_RANGE]: The value of "options.bufferSize" is out of range. It must be >= 1 && <= 4294967295. Received 0
at new Dir (node:internal/fs/dir:64:7)
at FSReqCallback.opendirCallback [as oncomplete] (node:internal/fs/dir:357:22)
Additional information
bufferSize is only validated in the Dir constructor, which fs.opendir() calls from the completion callback of the native call, so the error is thrown outside the caller's code. The handle is closed before the throw, so nothing leaks. I have a fix and will open a PR.