Skip to content

Commit

Permalink
fs: use consistent buffer array validation
Browse files Browse the repository at this point in the history
This commit updates fs.writev() and fs.writevSync() to use the
same validation method as filehandle.writev().

PR-URL: #29186
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
cjihrig authored and targos committed Aug 20, 2019
1 parent 81f3eb5 commit 82eeadb
Showing 1 changed file with 4 additions and 23 deletions.
27 changes: 4 additions & 23 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const {
stringToFlags,
stringToSymlinkType,
toUnixTimestamp,
validateBufferArray,
validateOffsetLengthRead,
validateOffsetLengthWrite,
validatePath,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 82eeadb

Please sign in to comment.