From 571eb6dc742b383579964cefb025477e3ab8a2d4 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Fri, 11 Feb 2022 12:32:10 +0200 Subject: [PATCH] fs: fix cb/sync writev empty array behavior PR-URL: https://github.com/nodejs/node/pull/41932 Refs: https://github.com/nodejs/node/issues/41910 Reviewed-By: Antoine du Hamel Reviewed-By: Robert Nagy Reviewed-By: James M Snell --- lib/fs.js | 9 +++++++++ test/parallel/test-fs-writev-sync.js | 12 +++++++++++- test/parallel/test-fs-writev.js | 21 ++++++++++++++++++++- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 9c0173ec27c1c0..75349f5b1f6f0e 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -907,6 +907,11 @@ function writev(fd, buffers, position, callback) { validateBufferArray(buffers); callback = maybeCallback(callback || position); + if (buffers.length === 0) { + process.nextTick(callback, null, 0, buffers); + return; + } + const req = new FSReqCallback(); req.oncomplete = wrapper; @@ -933,6 +938,10 @@ function writevSync(fd, buffers, position) { fd = getValidatedFd(fd); validateBufferArray(buffers); + if (buffers.length === 0) { + return 0; + } + const ctx = {}; if (typeof position !== 'number') diff --git a/test/parallel/test-fs-writev-sync.js b/test/parallel/test-fs-writev-sync.js index 33b76c7af25d9f..1db437972ed29d 100644 --- a/test/parallel/test-fs-writev-sync.js +++ b/test/parallel/test-fs-writev-sync.js @@ -56,11 +56,21 @@ const getFileName = (i) => path.join(tmpdir.path, `writev_sync_${i}.txt`); assert(Buffer.concat(bufferArr).equals(fs.readFileSync(filename))); } +// fs.writevSync with empty array of buffers +{ + const filename = getFileName(3); + const fd = fs.openSync(filename, 'w'); + const written = fs.writevSync(fd, []); + assert.strictEqual(written, 0); + fs.closeSync(fd); + +} + /** * Testing with wrong input types */ { - const filename = getFileName(3); + const filename = getFileName(4); const fd = fs.openSync(filename, 'w'); [false, 'test', {}, [{}], ['sdf'], null, undefined].forEach((i) => { diff --git a/test/parallel/test-fs-writev.js b/test/parallel/test-fs-writev.js index a516f9c328f1b2..7ea52ef77ac4ee 100644 --- a/test/parallel/test-fs-writev.js +++ b/test/parallel/test-fs-writev.js @@ -57,11 +57,30 @@ const getFileName = (i) => path.join(tmpdir.path, `writev_${i}.txt`); fs.writev(fd, bufferArr, done); } + +// fs.writev with empty array of buffers +{ + const filename = getFileName(3); + const fd = fs.openSync(filename, 'w'); + const bufferArr = []; + let afterSyncCall = false; + + const done = common.mustSucceed((written, buffers) => { + assert.strictEqual(buffers.length, 0); + assert.strictEqual(written, 0); + assert(afterSyncCall); + fs.closeSync(fd); + }); + + fs.writev(fd, bufferArr, done); + afterSyncCall = true; +} + /** * Testing with wrong input types */ { - const filename = getFileName(3); + const filename = getFileName(4); const fd = fs.openSync(filename, 'w'); [false, 'test', {}, [{}], ['sdf'], null, undefined].forEach((i) => {