fs: close dir before throwing if options.bufferSize is invalid

PR-URL: https://github.com/nodejs/node/pull/58856
Fixes: https://github.com/nodejs/node/issues/58854
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
This commit is contained in:
Livia Medeiros authored and RafaelGSS committed 2025-07-08 11:16:18 -03:00
1 parent 7f7a833e82
commit b378fc3ac0
2 files changed
+15 -1

No files matched your search

+7 -1
View File
@@ -59,7 +59,13 @@ class Dir {
}),
};
validateUint32(this.#options.bufferSize, 'options.bufferSize', true);
try {
validateUint32(this.#options.bufferSize, 'options.bufferSize', true);
} catch (validationError) {
// Userland won't be able to close handle if we throw, so we close it first
this.#handle.close();
throw validationError;
}
this.#readPromisified = FunctionPrototypeBind(
promisify(this.#readImpl), this, false);
+8
View File
@@ -4,12 +4,20 @@ const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const process = require('node:process');
const tmpdir = require('../common/tmpdir');
const testDir = tmpdir.path;
const files = ['empty', 'files', 'for', 'just', 'testing'];
process.on('warning', (cause) => {
// If any directory handle was left unclosed and then GC'd,
// it will emit `Warning: Closing directory handle on garbage collection`.
// Treat this warning as error.
throw new Error('Expected no warnings', { cause });
});
// Make sure tmp directory is clean
tmpdir.refresh();