Skip to content
Permalink
Browse files

fs: use consistent buffer array validation

This commit updates fs.writev() and fs.writevSync() to use the
same validation method as filehandle.writev().

PR-URL: #29186
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information...
cjihrig authored and targos committed Aug 17, 2019
1 parent 81f3eb5 commit 82eeadb21633f4c1ad3639c2b77c82af5b489051
Showing with 4 additions and 23 deletions.
  1. +4 −23 lib/fs.js
@@ -72,6 +72,7 @@ const {
stringToFlags,
stringToSymlinkType,
toUnixTimestamp,
validateBufferArray,
validateOffsetLengthRead,
validateOffsetLengthWrite,
validatePath,
@@ -142,19 +143,6 @@ function maybeCallback(cb) {
throw new ERR_INVALID_CALLBACK(cb);
}

function isBuffersArray(value) {
if (!Array.isArray(value))
return false;

for (var i = 0; i < value.length; i += 1) {
if (!isArrayBufferView(value[i])) {
return false;
}
}

return true;
}

// Ensure that callbacks run in the global context. Only use this function
// for callbacks that are passed to the binding layer, callbacks that are
// invoked from JS already run in the proper scope.
@@ -610,10 +598,7 @@ function writev(fd, buffers, position, callback) {
}

validateInt32(fd, 'fd', 0);

if (!isBuffersArray(buffers)) {
throw new ERR_INVALID_ARG_TYPE('buffers', 'ArrayBufferView[]', buffers);
}
validateBufferArray(buffers);

const req = new FSReqCallback();
req.oncomplete = wrapper;
@@ -631,15 +616,11 @@ Object.defineProperty(writev, internalUtil.customPromisifyArgs, {
enumerable: false
});

// fs.writevSync(fd, buffers[, position]);
function writevSync(fd, buffers, position) {

validateInt32(fd, 'fd', 0);
const ctx = {};
validateBufferArray(buffers);

if (!isBuffersArray(buffers)) {
throw new ERR_INVALID_ARG_TYPE('buffers', 'ArrayBufferView[]', buffers);
}
const ctx = {};

if (typeof position !== 'number')
position = null;

0 comments on commit 82eeadb

Please sign in to comment.
You can’t perform that action at this time.