Skip to content

Commit

Permalink
fs: fix cb/sync writev empty array behavior
Browse files Browse the repository at this point in the history
PR-URL: #41932
Refs: #41910
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Robert Nagy <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
benjamingr authored and bengl committed Feb 22, 2022
1 parent b8cbb83 commit be5143b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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')
Expand Down
12 changes: 11 additions & 1 deletion test/parallel/test-fs-writev-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
21 changes: 20 additions & 1 deletion test/parallel/test-fs-writev.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit be5143b

Please sign in to comment.