Skip to content

Commit

Permalink
fs: extract out validateUint32 and validateLen functions
Browse files Browse the repository at this point in the history
PR-URL: #17682
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
  • Loading branch information
maclover7 authored and joyeecheung committed Jan 11, 2018
1 parent 46e1d69 commit 8aec363
Showing 1 changed file with 54 additions and 62 deletions.
116 changes: 54 additions & 62 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,14 @@ function validateBuffer(buffer) {
}
}

function validateFd(fd) {
function validateLen(len) {
let err;

if (!isUint32(fd))
err = new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
if (!isInt32(len))
err = new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'integer');

if (err !== undefined) {
Error.captureStackTrace(err, validateFd);
Error.captureStackTrace(err, validateLen);
throw err;
}
}
Expand Down Expand Up @@ -222,6 +222,18 @@ function validatePath(path, propName) {
}
}

function validateUint32(value, propName) {
let err;

if (!isUint32(value))
err = new errors.TypeError('ERR_INVALID_ARG_TYPE', propName, 'integer');

if (err !== undefined) {
Error.captureStackTrace(err, validateUint32);
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.
Expand Down Expand Up @@ -708,14 +720,14 @@ fs.readFileSync = function(path, options) {
};

fs.close = function(fd, callback) {
validateFd(fd);
validateUint32(fd, 'fd');
const req = new FSReqWrap();
req.oncomplete = makeCallback(callback);
binding.close(fd, req);
};

fs.closeSync = function(fd) {
validateFd(fd);
validateUint32(fd, 'fd');

const ctx = {};
binding.close(fd, undefined, ctx);
Expand All @@ -742,9 +754,7 @@ fs.open = function(path, flags, mode, callback_) {
return;
if (!nullCheck(path, callback)) return;
validatePath(path);

if (!isUint32(mode))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
validateUint32(mode, 'mode');

const req = new FSReqWrap();
req.oncomplete = callback;
Expand All @@ -760,16 +770,14 @@ fs.openSync = function(path, flags, mode) {
handleError((path = getPathFromURL(path)));
nullCheck(path);
validatePath(path);

if (!isUint32(mode))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
validateUint32(mode, 'mode');

return binding.open(pathModule.toNamespacedPath(path),
stringToFlags(flags), mode);
};

fs.read = function(fd, buffer, offset, length, position, callback) {
validateFd(fd);
validateUint32(fd, 'fd');
validateBuffer(buffer);

offset |= 0;
Expand Down Expand Up @@ -801,7 +809,7 @@ Object.defineProperty(fs.read, internalUtil.customPromisifyArgs,
{ value: ['bytesRead', 'buffer'], enumerable: false });

fs.readSync = function(fd, buffer, offset, length, position) {
validateFd(fd);
validateUint32(fd, 'fd');
validateBuffer(buffer);

offset |= 0;
Expand Down Expand Up @@ -829,7 +837,7 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
callback(err, written || 0, buffer);
}

validateFd(fd);
validateUint32(fd, 'fd');

const req = new FSReqWrap();
req.oncomplete = wrapper;
Expand Down Expand Up @@ -869,7 +877,7 @@ Object.defineProperty(fs.write, internalUtil.customPromisifyArgs,
// OR
// fs.writeSync(fd, string[, position[, encoding]]);
fs.writeSync = function(fd, buffer, offset, length, position) {
validateFd(fd);
validateUint32(fd, 'fd');
if (isUint8Array(buffer)) {
if (position === undefined)
position = null;
Expand Down Expand Up @@ -968,19 +976,17 @@ fs.ftruncate = function(fd, len = 0, callback) {
callback = len;
len = 0;
}
validateFd(fd);
if (!isInt32(len))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'integer');
validateUint32(fd, 'fd');
validateLen(len);
len = Math.max(0, len);
const req = new FSReqWrap();
req.oncomplete = makeCallback(callback);
binding.ftruncate(fd, len, req);
};

fs.ftruncateSync = function(fd, len = 0) {
validateFd(fd);
if (!isInt32(len))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'integer');
validateUint32(fd, 'fd');
validateLen(len);
len = Math.max(0, len);
return binding.ftruncate(fd, len);
};
Expand All @@ -1004,26 +1010,26 @@ fs.rmdirSync = function(path) {
};

fs.fdatasync = function(fd, callback) {
validateFd(fd);
validateUint32(fd, 'fd');
const req = new FSReqWrap();
req.oncomplete = makeCallback(callback);
binding.fdatasync(fd, req);
};

fs.fdatasyncSync = function(fd) {
validateFd(fd);
validateUint32(fd, 'fd');
return binding.fdatasync(fd);
};

fs.fsync = function(fd, callback) {
validateFd(fd);
validateUint32(fd, 'fd');
const req = new FSReqWrap();
req.oncomplete = makeCallback(callback);
binding.fsync(fd, req);
};

fs.fsyncSync = function(fd) {
validateFd(fd);
validateUint32(fd, 'fd');
return binding.fsync(fd);
};

Expand All @@ -1036,8 +1042,7 @@ fs.mkdir = function(path, mode, callback) {

validatePath(path);
mode = modeNum(mode, 0o777);
if (!isUint32(mode))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
validateUint32(mode, 'mode');

const req = new FSReqWrap();
req.oncomplete = callback;
Expand All @@ -1049,8 +1054,7 @@ fs.mkdirSync = function(path, mode) {
nullCheck(path);
validatePath(path);
mode = modeNum(mode, 0o777);
if (!isUint32(mode))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
validateUint32(mode, 'mode');
return binding.mkdir(pathModule.toNamespacedPath(path), mode);
};

Expand All @@ -1077,7 +1081,7 @@ fs.readdirSync = function(path, options) {
};

fs.fstat = function(fd, callback) {
validateFd(fd);
validateUint32(fd, 'fd');
const req = new FSReqWrap();
req.oncomplete = makeStatsCallback(callback);
binding.fstat(fd, req);
Expand Down Expand Up @@ -1106,7 +1110,7 @@ fs.stat = function(path, callback) {
};

fs.fstatSync = function(fd) {
validateFd(fd);
validateUint32(fd, 'fd');
binding.fstat(fd);
return statsFromValues();
};
Expand Down Expand Up @@ -1273,9 +1277,8 @@ fs.unlinkSync = function(path) {

fs.fchmod = function(fd, mode, callback) {
mode = modeNum(mode);
validateFd(fd);
if (!isUint32(mode))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
validateUint32(fd, 'fd');
validateUint32(mode, 'mode');
if (mode < 0 || mode > 0o777)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'mode');

Expand All @@ -1286,9 +1289,8 @@ fs.fchmod = function(fd, mode, callback) {

fs.fchmodSync = function(fd, mode) {
mode = modeNum(mode);
validateFd(fd);
if (!isUint32(mode))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
validateUint32(fd, 'fd');
validateUint32(mode, 'mode');
if (mode < 0 || mode > 0o777)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'mode');
return binding.fchmod(fd, mode);
Expand Down Expand Up @@ -1340,8 +1342,7 @@ fs.chmod = function(path, mode, callback) {

validatePath(path);
mode = modeNum(mode);
if (!isUint32(mode))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
validateUint32(mode, 'mode');

const req = new FSReqWrap();
req.oncomplete = callback;
Expand All @@ -1353,8 +1354,7 @@ fs.chmodSync = function(path, mode) {
nullCheck(path);
validatePath(path);
mode = modeNum(mode);
if (!isUint32(mode))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
validateUint32(mode, 'mode');
return binding.chmod(pathModule.toNamespacedPath(path), mode);
};

Expand All @@ -1377,23 +1377,19 @@ if (constants.O_SYMLINK !== undefined) {
}

fs.fchown = function(fd, uid, gid, callback) {
validateFd(fd);
if (!isUint32(uid))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'integer');
if (!isUint32(gid))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'gid', 'integer');
validateUint32(fd, 'fd');
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');

const req = new FSReqWrap();
req.oncomplete = makeCallback(callback);
binding.fchown(fd, uid, gid, req);
};

fs.fchownSync = function(fd, uid, gid) {
validateFd(fd);
if (!isUint32(uid))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'integer');
if (!isUint32(gid))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'gid', 'integer');
validateUint32(fd, 'fd');
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');

return binding.fchown(fd, uid, gid);
};
Expand All @@ -1405,10 +1401,8 @@ fs.chown = function(path, uid, gid, callback) {
if (!nullCheck(path, callback)) return;

validatePath(path);
if (!isUint32(uid))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'integer');
if (!isUint32(gid))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'gid', 'integer');
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');

const req = new FSReqWrap();
req.oncomplete = callback;
Expand All @@ -1419,10 +1413,8 @@ fs.chownSync = function(path, uid, gid) {
handleError((path = getPathFromURL(path)));
nullCheck(path);
validatePath(path);
if (!isUint32(uid))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'integer');
if (!isUint32(gid))
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'gid', 'integer');
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
return binding.chown(pathModule.toNamespacedPath(path), uid, gid);
};

Expand Down Expand Up @@ -1477,7 +1469,7 @@ fs.utimesSync = function(path, atime, mtime) {
};

fs.futimes = function(fd, atime, mtime, callback) {
validateFd(fd);
validateUint32(fd, 'fd');
atime = toUnixTimestamp(atime, 'atime');
mtime = toUnixTimestamp(mtime, 'mtime');
const req = new FSReqWrap();
Expand All @@ -1486,7 +1478,7 @@ fs.futimes = function(fd, atime, mtime, callback) {
};

fs.futimesSync = function(fd, atime, mtime) {
validateFd(fd);
validateUint32(fd, 'fd');
atime = toUnixTimestamp(atime, 'atime');
mtime = toUnixTimestamp(mtime, 'mtime');
binding.futimes(fd, atime, mtime);
Expand Down

0 comments on commit 8aec363

Please sign in to comment.