diff --git a/lib/internal/fs/read/context.js b/lib/internal/fs/read/context.js index 270f83323d5d52..3d11140a5ce24c 100644 --- a/lib/internal/fs/read/context.js +++ b/lib/internal/fs/read/context.js @@ -180,7 +180,7 @@ class ReadFileContext { close(err) { if (this.isUserFd) { process.nextTick(function tick(context) { - FunctionPrototypeCall(readFileAfterClose, { context }, null); + FunctionPrototypeCall(readFileAfterClose, { context }, err); }, this); return; } diff --git a/test/parallel/test-fs-readfile-buffer-option.js b/test/parallel/test-fs-readfile-buffer-option.js index 27ce6be136b56c..2ac28bfbd6a0db 100644 --- a/test/parallel/test-fs-readfile-buffer-option.js +++ b/test/parallel/test-fs-readfile-buffer-option.js @@ -24,6 +24,15 @@ function readFile(path, options) { }); } +function readFileFd(fd, options) { + return new Promise((resolve, reject) => { + fs.readFile(fd, options, (err, data) => { + if (err) reject(err); + else resolve(data); + }); + }); +} + async function withFstatSizeZero(fn) { const originalFstat = fsBinding.fstat; fsBinding.fstat = function(...args) { @@ -128,6 +137,32 @@ async function withFstatSizeZero(fn) { code: 'ERR_INVALID_ARG_VALUE', }); + { + const fd = fs.openSync(file, 'r'); + try { + const buffer = Buffer.alloc(content.length - 1); + await assert.rejects(readFileFd(fd, { buffer }), { + code: 'ERR_INVALID_ARG_VALUE', + }); + } finally { + fs.closeSync(fd); + } + } + + { + const fd = fs.openSync(file, 'r'); + try { + await assert.rejects(readFileFd(fd, { + buffer() { + return Buffer.alloc(content.length - 1); + }, + }), { + code: 'ERR_INVALID_ARG_VALUE', + }); + } finally { + fs.closeSync(fd); + } + } await withFstatSizeZero(common.mustCall(async () => { {