From b4db004855bb2e6d999ea6683b737f5006fea0ef Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Sat, 12 Feb 2022 19:57:52 +0200 Subject: [PATCH] fs: fix writev empty array error behavior PR-URL: https://github.com/nodejs/node/pull/41919 Fixes: https://github.com/nodejs/node/issues/41910 Reviewed-By: Antoine du Hamel Reviewed-By: Robert Nagy Reviewed-By: Minwoo Jung --- lib/internal/fs/promises.js | 4 ++++ test/parallel/test-fs-writev-promises.js | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 6a0ce4d05c6b87..fbc4de640ade3c 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -596,6 +596,10 @@ async function writev(handle, buffers, position) { if (typeof position !== 'number') position = null; + if (buffers.length === 0) { + return { bytesWritten: 0, buffers }; + } + const bytesWritten = (await binding.writeBuffers(handle.fd, buffers, position, kUsePromises)) || 0; return { bytesWritten, buffers }; diff --git a/test/parallel/test-fs-writev-promises.js b/test/parallel/test-fs-writev-promises.js index 7c610adb4e50c2..650f00dae74b69 100644 --- a/test/parallel/test-fs-writev-promises.js +++ b/test/parallel/test-fs-writev-promises.js @@ -47,4 +47,13 @@ tmpdir.refresh(); assert(Buffer.concat(bufferArr).equals(await fs.readFile(filename))); handle.close(); } + + { + // Writev with empty array behavior + const handle = await fs.open(getFileName(), 'w'); + const result = await handle.writev([]); + assert.strictEqual(result.bytesWritten, 0); + assert.strictEqual(result.buffers.length, 0); + handle.close(); + } })().then(common.mustCall());