From b9b8294ddad4baa7fbe565e5d18f18d815d7f967 Mon Sep 17 00:00:00 2001 From: Jon Moss Date: Wed, 13 Dec 2017 17:24:56 -0500 Subject: [PATCH] fs: extract out validateFd function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/17682 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Anatoli Papirovski Reviewed-By: Joyee Cheung Reviewed-By: Matteo Collina Reviewed-By: Tobias Nießen --- lib/fs.js | 73 ++++++++++++++++++++++++------------------------------- 1 file changed, 32 insertions(+), 41 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 8779bfe1b0957a..2a1fa8c5a3f379 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -153,6 +153,18 @@ function makeCallback(cb) { }; } +function validateFd(fd) { + let err; + + if (!isUint32(fd)) + err = new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + + if (err !== undefined) { + Error.captureStackTrace(err, validateFd); + throw err; + } +} + // Special case of `makeCallback()` that is specific to async `*stat()` calls as // an optimization, since the data passed back to the callback needs to be // transformed anyway. @@ -649,17 +661,14 @@ fs.readFileSync = function(path, options) { }; fs.close = function(fd, callback) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); - + validateFd(fd); const req = new FSReqWrap(); req.oncomplete = makeCallback(callback); binding.close(fd, req); }; fs.closeSync = function(fd) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); const ctx = {}; binding.close(fd, undefined, ctx); @@ -720,8 +729,7 @@ fs.openSync = function(path, flags, mode) { }; fs.read = function(fd, buffer, offset, length, position, callback) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); if (!isUint8Array(buffer)) throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'buffer', ['Buffer', 'Uint8Array']); @@ -759,8 +767,7 @@ Object.defineProperty(fs.read, internalUtil.customPromisifyArgs, { value: ['bytesRead', 'buffer'], enumerable: false }); fs.readSync = function(fd, buffer, offset, length, position) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); if (!isUint8Array(buffer)) throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'buffer', ['Buffer', 'Uint8Array']); @@ -794,8 +801,7 @@ fs.write = function(fd, buffer, offset, length, position, callback) { callback(err, written || 0, buffer); } - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); const req = new FSReqWrap(); req.oncomplete = wrapper; @@ -839,8 +845,7 @@ Object.defineProperty(fs.write, internalUtil.customPromisifyArgs, // OR // fs.writeSync(fd, string[, position[, encoding]]); fs.writeSync = function(fd, buffer, offset, length, position) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); if (isUint8Array(buffer)) { if (position === undefined) position = null; @@ -956,8 +961,7 @@ fs.ftruncate = function(fd, len = 0, callback) { callback = len; len = 0; } - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); if (!isInt32(len)) throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'integer'); len = Math.max(0, len); @@ -967,8 +971,7 @@ fs.ftruncate = function(fd, len = 0, callback) { }; fs.ftruncateSync = function(fd, len = 0) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); if (!isInt32(len)) throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'integer'); len = Math.max(0, len); @@ -1000,30 +1003,26 @@ fs.rmdirSync = function(path) { }; fs.fdatasync = function(fd, callback) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); const req = new FSReqWrap(); req.oncomplete = makeCallback(callback); binding.fdatasync(fd, req); }; fs.fdatasyncSync = function(fd) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); return binding.fdatasync(fd); }; fs.fsync = function(fd, callback) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); const req = new FSReqWrap(); req.oncomplete = makeCallback(callback); binding.fsync(fd, req); }; fs.fsyncSync = function(fd) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); return binding.fsync(fd); }; @@ -1089,8 +1088,7 @@ fs.readdirSync = function(path, options) { }; fs.fstat = function(fd, callback) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); const req = new FSReqWrap(); req.oncomplete = makeStatsCallback(callback); binding.fstat(fd, req); @@ -1125,8 +1123,7 @@ fs.stat = function(path, callback) { }; fs.fstatSync = function(fd) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); binding.fstat(fd); return statsFromValues(); }; @@ -1336,8 +1333,7 @@ fs.unlinkSync = function(path) { fs.fchmod = function(fd, mode, callback) { mode = modeNum(mode); - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); if (!isUint32(mode)) throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer'); if (mode < 0 || mode > 0o777) @@ -1350,8 +1346,7 @@ fs.fchmod = function(fd, mode, callback) { fs.fchmodSync = function(fd, mode) { mode = modeNum(mode); - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); if (!isUint32(mode)) throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer'); if (mode < 0 || mode > 0o777) @@ -1448,8 +1443,7 @@ if (constants.O_SYMLINK !== undefined) { } fs.fchown = function(fd, uid, gid, callback) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); if (!isUint32(uid)) throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'integer'); if (!isUint32(gid)) @@ -1461,8 +1455,7 @@ fs.fchown = function(fd, uid, gid, callback) { }; fs.fchownSync = function(fd, uid, gid) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); if (!isUint32(uid)) throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'integer'); if (!isUint32(gid)) @@ -1562,8 +1555,7 @@ fs.utimesSync = function(path, atime, mtime) { }; fs.futimes = function(fd, atime, mtime, callback) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); atime = toUnixTimestamp(atime, 'atime'); mtime = toUnixTimestamp(mtime, 'mtime'); const req = new FSReqWrap(); @@ -1572,8 +1564,7 @@ fs.futimes = function(fd, atime, mtime, callback) { }; fs.futimesSync = function(fd, atime, mtime) { - if (!isUint32(fd)) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer'); + validateFd(fd); atime = toUnixTimestamp(atime, 'atime'); mtime = toUnixTimestamp(mtime, 'mtime'); binding.futimes(fd, atime, mtime);