fs: extract out validateUint32 and validateLen functions
PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
46e1d69bd1
commit
8aec3638ce
116
lib/fs.js
116
lib/fs.js
@ -162,14 +162,14 @@ function validateBuffer(buffer) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function validateFd(fd) {
|
function validateLen(len) {
|
||||||
let err;
|
let err;
|
||||||
|
|
||||||
if (!isUint32(fd))
|
if (!isInt32(len))
|
||||||
err = new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
|
err = new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'integer');
|
||||||
|
|
||||||
if (err !== undefined) {
|
if (err !== undefined) {
|
||||||
Error.captureStackTrace(err, validateFd);
|
Error.captureStackTrace(err, validateLen);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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
|
// 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
|
// an optimization, since the data passed back to the callback needs to be
|
||||||
// transformed anyway.
|
// transformed anyway.
|
||||||
@ -708,14 +720,14 @@ fs.readFileSync = function(path, options) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fs.close = function(fd, callback) {
|
fs.close = function(fd, callback) {
|
||||||
validateFd(fd);
|
validateUint32(fd, 'fd');
|
||||||
const req = new FSReqWrap();
|
const req = new FSReqWrap();
|
||||||
req.oncomplete = makeCallback(callback);
|
req.oncomplete = makeCallback(callback);
|
||||||
binding.close(fd, req);
|
binding.close(fd, req);
|
||||||
};
|
};
|
||||||
|
|
||||||
fs.closeSync = function(fd) {
|
fs.closeSync = function(fd) {
|
||||||
validateFd(fd);
|
validateUint32(fd, 'fd');
|
||||||
|
|
||||||
const ctx = {};
|
const ctx = {};
|
||||||
binding.close(fd, undefined, ctx);
|
binding.close(fd, undefined, ctx);
|
||||||
@ -742,9 +754,7 @@ fs.open = function(path, flags, mode, callback_) {
|
|||||||
return;
|
return;
|
||||||
if (!nullCheck(path, callback)) return;
|
if (!nullCheck(path, callback)) return;
|
||||||
validatePath(path);
|
validatePath(path);
|
||||||
|
validateUint32(mode, 'mode');
|
||||||
if (!isUint32(mode))
|
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
|
|
||||||
|
|
||||||
const req = new FSReqWrap();
|
const req = new FSReqWrap();
|
||||||
req.oncomplete = callback;
|
req.oncomplete = callback;
|
||||||
@ -760,16 +770,14 @@ fs.openSync = function(path, flags, mode) {
|
|||||||
handleError((path = getPathFromURL(path)));
|
handleError((path = getPathFromURL(path)));
|
||||||
nullCheck(path);
|
nullCheck(path);
|
||||||
validatePath(path);
|
validatePath(path);
|
||||||
|
validateUint32(mode, 'mode');
|
||||||
if (!isUint32(mode))
|
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
|
|
||||||
|
|
||||||
return binding.open(pathModule.toNamespacedPath(path),
|
return binding.open(pathModule.toNamespacedPath(path),
|
||||||
stringToFlags(flags), mode);
|
stringToFlags(flags), mode);
|
||||||
};
|
};
|
||||||
|
|
||||||
fs.read = function(fd, buffer, offset, length, position, callback) {
|
fs.read = function(fd, buffer, offset, length, position, callback) {
|
||||||
validateFd(fd);
|
validateUint32(fd, 'fd');
|
||||||
validateBuffer(buffer);
|
validateBuffer(buffer);
|
||||||
|
|
||||||
offset |= 0;
|
offset |= 0;
|
||||||
@ -801,7 +809,7 @@ Object.defineProperty(fs.read, internalUtil.customPromisifyArgs,
|
|||||||
{ value: ['bytesRead', 'buffer'], enumerable: false });
|
{ value: ['bytesRead', 'buffer'], enumerable: false });
|
||||||
|
|
||||||
fs.readSync = function(fd, buffer, offset, length, position) {
|
fs.readSync = function(fd, buffer, offset, length, position) {
|
||||||
validateFd(fd);
|
validateUint32(fd, 'fd');
|
||||||
validateBuffer(buffer);
|
validateBuffer(buffer);
|
||||||
|
|
||||||
offset |= 0;
|
offset |= 0;
|
||||||
@ -829,7 +837,7 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
|
|||||||
callback(err, written || 0, buffer);
|
callback(err, written || 0, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
validateFd(fd);
|
validateUint32(fd, 'fd');
|
||||||
|
|
||||||
const req = new FSReqWrap();
|
const req = new FSReqWrap();
|
||||||
req.oncomplete = wrapper;
|
req.oncomplete = wrapper;
|
||||||
@ -869,7 +877,7 @@ Object.defineProperty(fs.write, internalUtil.customPromisifyArgs,
|
|||||||
// OR
|
// OR
|
||||||
// fs.writeSync(fd, string[, position[, encoding]]);
|
// fs.writeSync(fd, string[, position[, encoding]]);
|
||||||
fs.writeSync = function(fd, buffer, offset, length, position) {
|
fs.writeSync = function(fd, buffer, offset, length, position) {
|
||||||
validateFd(fd);
|
validateUint32(fd, 'fd');
|
||||||
if (isUint8Array(buffer)) {
|
if (isUint8Array(buffer)) {
|
||||||
if (position === undefined)
|
if (position === undefined)
|
||||||
position = null;
|
position = null;
|
||||||
@ -968,9 +976,8 @@ fs.ftruncate = function(fd, len = 0, callback) {
|
|||||||
callback = len;
|
callback = len;
|
||||||
len = 0;
|
len = 0;
|
||||||
}
|
}
|
||||||
validateFd(fd);
|
validateUint32(fd, 'fd');
|
||||||
if (!isInt32(len))
|
validateLen(len);
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'integer');
|
|
||||||
len = Math.max(0, len);
|
len = Math.max(0, len);
|
||||||
const req = new FSReqWrap();
|
const req = new FSReqWrap();
|
||||||
req.oncomplete = makeCallback(callback);
|
req.oncomplete = makeCallback(callback);
|
||||||
@ -978,9 +985,8 @@ fs.ftruncate = function(fd, len = 0, callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fs.ftruncateSync = function(fd, len = 0) {
|
fs.ftruncateSync = function(fd, len = 0) {
|
||||||
validateFd(fd);
|
validateUint32(fd, 'fd');
|
||||||
if (!isInt32(len))
|
validateLen(len);
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'integer');
|
|
||||||
len = Math.max(0, len);
|
len = Math.max(0, len);
|
||||||
return binding.ftruncate(fd, len);
|
return binding.ftruncate(fd, len);
|
||||||
};
|
};
|
||||||
@ -1004,26 +1010,26 @@ fs.rmdirSync = function(path) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fs.fdatasync = function(fd, callback) {
|
fs.fdatasync = function(fd, callback) {
|
||||||
validateFd(fd);
|
validateUint32(fd, 'fd');
|
||||||
const req = new FSReqWrap();
|
const req = new FSReqWrap();
|
||||||
req.oncomplete = makeCallback(callback);
|
req.oncomplete = makeCallback(callback);
|
||||||
binding.fdatasync(fd, req);
|
binding.fdatasync(fd, req);
|
||||||
};
|
};
|
||||||
|
|
||||||
fs.fdatasyncSync = function(fd) {
|
fs.fdatasyncSync = function(fd) {
|
||||||
validateFd(fd);
|
validateUint32(fd, 'fd');
|
||||||
return binding.fdatasync(fd);
|
return binding.fdatasync(fd);
|
||||||
};
|
};
|
||||||
|
|
||||||
fs.fsync = function(fd, callback) {
|
fs.fsync = function(fd, callback) {
|
||||||
validateFd(fd);
|
validateUint32(fd, 'fd');
|
||||||
const req = new FSReqWrap();
|
const req = new FSReqWrap();
|
||||||
req.oncomplete = makeCallback(callback);
|
req.oncomplete = makeCallback(callback);
|
||||||
binding.fsync(fd, req);
|
binding.fsync(fd, req);
|
||||||
};
|
};
|
||||||
|
|
||||||
fs.fsyncSync = function(fd) {
|
fs.fsyncSync = function(fd) {
|
||||||
validateFd(fd);
|
validateUint32(fd, 'fd');
|
||||||
return binding.fsync(fd);
|
return binding.fsync(fd);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1036,8 +1042,7 @@ fs.mkdir = function(path, mode, callback) {
|
|||||||
|
|
||||||
validatePath(path);
|
validatePath(path);
|
||||||
mode = modeNum(mode, 0o777);
|
mode = modeNum(mode, 0o777);
|
||||||
if (!isUint32(mode))
|
validateUint32(mode, 'mode');
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
|
|
||||||
|
|
||||||
const req = new FSReqWrap();
|
const req = new FSReqWrap();
|
||||||
req.oncomplete = callback;
|
req.oncomplete = callback;
|
||||||
@ -1049,8 +1054,7 @@ fs.mkdirSync = function(path, mode) {
|
|||||||
nullCheck(path);
|
nullCheck(path);
|
||||||
validatePath(path);
|
validatePath(path);
|
||||||
mode = modeNum(mode, 0o777);
|
mode = modeNum(mode, 0o777);
|
||||||
if (!isUint32(mode))
|
validateUint32(mode, 'mode');
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
|
|
||||||
return binding.mkdir(pathModule.toNamespacedPath(path), mode);
|
return binding.mkdir(pathModule.toNamespacedPath(path), mode);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1077,7 +1081,7 @@ fs.readdirSync = function(path, options) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fs.fstat = function(fd, callback) {
|
fs.fstat = function(fd, callback) {
|
||||||
validateFd(fd);
|
validateUint32(fd, 'fd');
|
||||||
const req = new FSReqWrap();
|
const req = new FSReqWrap();
|
||||||
req.oncomplete = makeStatsCallback(callback);
|
req.oncomplete = makeStatsCallback(callback);
|
||||||
binding.fstat(fd, req);
|
binding.fstat(fd, req);
|
||||||
@ -1106,7 +1110,7 @@ fs.stat = function(path, callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fs.fstatSync = function(fd) {
|
fs.fstatSync = function(fd) {
|
||||||
validateFd(fd);
|
validateUint32(fd, 'fd');
|
||||||
binding.fstat(fd);
|
binding.fstat(fd);
|
||||||
return statsFromValues();
|
return statsFromValues();
|
||||||
};
|
};
|
||||||
@ -1273,9 +1277,8 @@ fs.unlinkSync = function(path) {
|
|||||||
|
|
||||||
fs.fchmod = function(fd, mode, callback) {
|
fs.fchmod = function(fd, mode, callback) {
|
||||||
mode = modeNum(mode);
|
mode = modeNum(mode);
|
||||||
validateFd(fd);
|
validateUint32(fd, 'fd');
|
||||||
if (!isUint32(mode))
|
validateUint32(mode, 'mode');
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
|
|
||||||
if (mode < 0 || mode > 0o777)
|
if (mode < 0 || mode > 0o777)
|
||||||
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'mode');
|
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'mode');
|
||||||
|
|
||||||
@ -1286,9 +1289,8 @@ fs.fchmod = function(fd, mode, callback) {
|
|||||||
|
|
||||||
fs.fchmodSync = function(fd, mode) {
|
fs.fchmodSync = function(fd, mode) {
|
||||||
mode = modeNum(mode);
|
mode = modeNum(mode);
|
||||||
validateFd(fd);
|
validateUint32(fd, 'fd');
|
||||||
if (!isUint32(mode))
|
validateUint32(mode, 'mode');
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
|
|
||||||
if (mode < 0 || mode > 0o777)
|
if (mode < 0 || mode > 0o777)
|
||||||
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'mode');
|
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'mode');
|
||||||
return binding.fchmod(fd, mode);
|
return binding.fchmod(fd, mode);
|
||||||
@ -1340,8 +1342,7 @@ fs.chmod = function(path, mode, callback) {
|
|||||||
|
|
||||||
validatePath(path);
|
validatePath(path);
|
||||||
mode = modeNum(mode);
|
mode = modeNum(mode);
|
||||||
if (!isUint32(mode))
|
validateUint32(mode, 'mode');
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
|
|
||||||
|
|
||||||
const req = new FSReqWrap();
|
const req = new FSReqWrap();
|
||||||
req.oncomplete = callback;
|
req.oncomplete = callback;
|
||||||
@ -1353,8 +1354,7 @@ fs.chmodSync = function(path, mode) {
|
|||||||
nullCheck(path);
|
nullCheck(path);
|
||||||
validatePath(path);
|
validatePath(path);
|
||||||
mode = modeNum(mode);
|
mode = modeNum(mode);
|
||||||
if (!isUint32(mode))
|
validateUint32(mode, 'mode');
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
|
|
||||||
return binding.chmod(pathModule.toNamespacedPath(path), mode);
|
return binding.chmod(pathModule.toNamespacedPath(path), mode);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1377,11 +1377,9 @@ if (constants.O_SYMLINK !== undefined) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fs.fchown = function(fd, uid, gid, callback) {
|
fs.fchown = function(fd, uid, gid, callback) {
|
||||||
validateFd(fd);
|
validateUint32(fd, 'fd');
|
||||||
if (!isUint32(uid))
|
validateUint32(uid, 'uid');
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'integer');
|
validateUint32(gid, 'gid');
|
||||||
if (!isUint32(gid))
|
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'gid', 'integer');
|
|
||||||
|
|
||||||
const req = new FSReqWrap();
|
const req = new FSReqWrap();
|
||||||
req.oncomplete = makeCallback(callback);
|
req.oncomplete = makeCallback(callback);
|
||||||
@ -1389,11 +1387,9 @@ fs.fchown = function(fd, uid, gid, callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fs.fchownSync = function(fd, uid, gid) {
|
fs.fchownSync = function(fd, uid, gid) {
|
||||||
validateFd(fd);
|
validateUint32(fd, 'fd');
|
||||||
if (!isUint32(uid))
|
validateUint32(uid, 'uid');
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'integer');
|
validateUint32(gid, 'gid');
|
||||||
if (!isUint32(gid))
|
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'gid', 'integer');
|
|
||||||
|
|
||||||
return binding.fchown(fd, uid, gid);
|
return binding.fchown(fd, uid, gid);
|
||||||
};
|
};
|
||||||
@ -1405,10 +1401,8 @@ fs.chown = function(path, uid, gid, callback) {
|
|||||||
if (!nullCheck(path, callback)) return;
|
if (!nullCheck(path, callback)) return;
|
||||||
|
|
||||||
validatePath(path);
|
validatePath(path);
|
||||||
if (!isUint32(uid))
|
validateUint32(uid, 'uid');
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'integer');
|
validateUint32(gid, 'gid');
|
||||||
if (!isUint32(gid))
|
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'gid', 'integer');
|
|
||||||
|
|
||||||
const req = new FSReqWrap();
|
const req = new FSReqWrap();
|
||||||
req.oncomplete = callback;
|
req.oncomplete = callback;
|
||||||
@ -1419,10 +1413,8 @@ fs.chownSync = function(path, uid, gid) {
|
|||||||
handleError((path = getPathFromURL(path)));
|
handleError((path = getPathFromURL(path)));
|
||||||
nullCheck(path);
|
nullCheck(path);
|
||||||
validatePath(path);
|
validatePath(path);
|
||||||
if (!isUint32(uid))
|
validateUint32(uid, 'uid');
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'integer');
|
validateUint32(gid, 'gid');
|
||||||
if (!isUint32(gid))
|
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'gid', 'integer');
|
|
||||||
return binding.chown(pathModule.toNamespacedPath(path), uid, gid);
|
return binding.chown(pathModule.toNamespacedPath(path), uid, gid);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1477,7 +1469,7 @@ fs.utimesSync = function(path, atime, mtime) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fs.futimes = function(fd, atime, mtime, callback) {
|
fs.futimes = function(fd, atime, mtime, callback) {
|
||||||
validateFd(fd);
|
validateUint32(fd, 'fd');
|
||||||
atime = toUnixTimestamp(atime, 'atime');
|
atime = toUnixTimestamp(atime, 'atime');
|
||||||
mtime = toUnixTimestamp(mtime, 'mtime');
|
mtime = toUnixTimestamp(mtime, 'mtime');
|
||||||
const req = new FSReqWrap();
|
const req = new FSReqWrap();
|
||||||
@ -1486,7 +1478,7 @@ fs.futimes = function(fd, atime, mtime, callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fs.futimesSync = function(fd, atime, mtime) {
|
fs.futimesSync = function(fd, atime, mtime) {
|
||||||
validateFd(fd);
|
validateUint32(fd, 'fd');
|
||||||
atime = toUnixTimestamp(atime, 'atime');
|
atime = toUnixTimestamp(atime, 'atime');
|
||||||
mtime = toUnixTimestamp(mtime, 'mtime');
|
mtime = toUnixTimestamp(mtime, 'mtime');
|
||||||
binding.futimes(fd, atime, mtime);
|
binding.futimes(fd, atime, mtime);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user