Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: fix cb/sync writev empty array behavior #41932

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/fs.js
Expand Up @@ -906,6 +906,11 @@ function writev(fd, buffers, position, callback) {
validateBufferArray(buffers);
callback = maybeCallback(callback || position);

if (buffers.length === 0) {
callback(null, 0, buffers);
benjamingr marked this conversation as resolved.
Show resolved Hide resolved
return;
}

const req = new FSReqCallback();
req.oncomplete = wrapper;

Expand All @@ -932,6 +937,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
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
18 changes: 17 additions & 1 deletion test/parallel/test-fs-writev.js
Expand Up @@ -57,11 +57,27 @@ 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 = [];

const done = common.mustSucceed((written, buffers) => {
assert.strictEqual(buffers.length, 0);
assert.strictEqual(written, 0);
fs.closeSync(fd);
});

fs.writev(fd, bufferArr, done);
}

/**
* 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