Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions lib/internal/fs/dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
ArrayPrototypeShift,
FunctionPrototypeBind,
ObjectDefineProperties,
ObjectHasOwn,
PromiseReject,
SymbolAsyncDispose,
SymbolAsyncIterator,
Expand Down Expand Up @@ -350,6 +351,12 @@ function opendir(path, options, callback) {
encoding: 'utf8',
});

// Validate bufferSize before opening the directory. The Dir constructor
// validates it too, but an error thrown from opendirCallback can't be caught.
if (ObjectHasOwn(options, 'bufferSize')) {
validateUint32(options.bufferSize, 'options.bufferSize', true);
}

function opendirCallback(error, handle) {
if (error) {
callback(error);
Expand Down
36 changes: 36 additions & 0 deletions test/parallel/test-fs-opendir-invalid-buffersize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

// This tests that fs.opendir() and fs.promises.opendir() report an invalid
// bufferSize to the caller instead of throwing from an internal callback.

const common = require('../common');
const assert = require('assert');
const fs = require('fs');

const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

for (const bufferSize of [-1, 0, 0.5, 1.5, Infinity, NaN]) {
const expected = { code: 'ERR_OUT_OF_RANGE', name: 'RangeError' };
assert.throws(
() => fs.opendir(tmpdir.path, { bufferSize }, common.mustNotCall()),
expected,
);
assert.rejects(fs.promises.opendir(tmpdir.path, { bufferSize }), expected)
.then(common.mustCall());
}

for (const bufferSize of ['', '1', null, undefined]) {
const expected = { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError' };
assert.throws(
() => fs.opendir(tmpdir.path, { bufferSize }, common.mustNotCall()),
expected,
);
assert.rejects(fs.promises.opendir(tmpdir.path, { bufferSize }), expected)
.then(common.mustCall());
}

// A valid bufferSize still opens the directory.
fs.opendir(tmpdir.path, { bufferSize: 1 }, common.mustSucceed((dir) => {
dir.closeSync();
}));