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 2025-07-06 13:32:45 +08:00 committed by GitHub
parent b890c51ff2
commit 9ab976397d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 1 deletions

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);

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();