From 8d100c21c5b7c4687b0fc7f36aeed0a517dca1f7 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sat, 17 Aug 2019 15:03:46 -0400 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/29186 Reviewed-By: Anna Henningsen Reviewed-By: Rich Trott Reviewed-By: James M Snell --- lib/fs.js | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 2c10a5c8d06fc1..37d1eb7917bca1 100644 --- a/lib/fs.js +++ b/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. @@ -626,10 +614,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; @@ -647,15 +632,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;