Skip to content

Commit b9b8294

Browse files
maclover7joyeecheung
authored andcommitted
fs: extract out validateFd function
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>
1 parent 6e3818f commit b9b8294

File tree

1 file changed

+32
-41
lines changed

1 file changed

+32
-41
lines changed

lib/fs.js

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,18 @@ function makeCallback(cb) {
153153
};
154154
}
155155

156+
function validateFd(fd) {
157+
let err;
158+
159+
if (!isUint32(fd))
160+
err = new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
161+
162+
if (err !== undefined) {
163+
Error.captureStackTrace(err, validateFd);
164+
throw err;
165+
}
166+
}
167+
156168
// Special case of `makeCallback()` that is specific to async `*stat()` calls as
157169
// an optimization, since the data passed back to the callback needs to be
158170
// transformed anyway.
@@ -649,17 +661,14 @@ fs.readFileSync = function(path, options) {
649661
};
650662

651663
fs.close = function(fd, callback) {
652-
if (!isUint32(fd))
653-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
654-
664+
validateFd(fd);
655665
const req = new FSReqWrap();
656666
req.oncomplete = makeCallback(callback);
657667
binding.close(fd, req);
658668
};
659669

660670
fs.closeSync = function(fd) {
661-
if (!isUint32(fd))
662-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
671+
validateFd(fd);
663672

664673
const ctx = {};
665674
binding.close(fd, undefined, ctx);
@@ -720,8 +729,7 @@ fs.openSync = function(path, flags, mode) {
720729
};
721730

722731
fs.read = function(fd, buffer, offset, length, position, callback) {
723-
if (!isUint32(fd))
724-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
732+
validateFd(fd);
725733
if (!isUint8Array(buffer))
726734
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'buffer',
727735
['Buffer', 'Uint8Array']);
@@ -759,8 +767,7 @@ Object.defineProperty(fs.read, internalUtil.customPromisifyArgs,
759767
{ value: ['bytesRead', 'buffer'], enumerable: false });
760768

761769
fs.readSync = function(fd, buffer, offset, length, position) {
762-
if (!isUint32(fd))
763-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
770+
validateFd(fd);
764771
if (!isUint8Array(buffer))
765772
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'buffer',
766773
['Buffer', 'Uint8Array']);
@@ -794,8 +801,7 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
794801
callback(err, written || 0, buffer);
795802
}
796803

797-
if (!isUint32(fd))
798-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
804+
validateFd(fd);
799805

800806
const req = new FSReqWrap();
801807
req.oncomplete = wrapper;
@@ -839,8 +845,7 @@ Object.defineProperty(fs.write, internalUtil.customPromisifyArgs,
839845
// OR
840846
// fs.writeSync(fd, string[, position[, encoding]]);
841847
fs.writeSync = function(fd, buffer, offset, length, position) {
842-
if (!isUint32(fd))
843-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
848+
validateFd(fd);
844849
if (isUint8Array(buffer)) {
845850
if (position === undefined)
846851
position = null;
@@ -956,8 +961,7 @@ fs.ftruncate = function(fd, len = 0, callback) {
956961
callback = len;
957962
len = 0;
958963
}
959-
if (!isUint32(fd))
960-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
964+
validateFd(fd);
961965
if (!isInt32(len))
962966
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'integer');
963967
len = Math.max(0, len);
@@ -967,8 +971,7 @@ fs.ftruncate = function(fd, len = 0, callback) {
967971
};
968972

969973
fs.ftruncateSync = function(fd, len = 0) {
970-
if (!isUint32(fd))
971-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
974+
validateFd(fd);
972975
if (!isInt32(len))
973976
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'integer');
974977
len = Math.max(0, len);
@@ -1000,30 +1003,26 @@ fs.rmdirSync = function(path) {
10001003
};
10011004

10021005
fs.fdatasync = function(fd, callback) {
1003-
if (!isUint32(fd))
1004-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
1006+
validateFd(fd);
10051007
const req = new FSReqWrap();
10061008
req.oncomplete = makeCallback(callback);
10071009
binding.fdatasync(fd, req);
10081010
};
10091011

10101012
fs.fdatasyncSync = function(fd) {
1011-
if (!isUint32(fd))
1012-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
1013+
validateFd(fd);
10131014
return binding.fdatasync(fd);
10141015
};
10151016

10161017
fs.fsync = function(fd, callback) {
1017-
if (!isUint32(fd))
1018-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
1018+
validateFd(fd);
10191019
const req = new FSReqWrap();
10201020
req.oncomplete = makeCallback(callback);
10211021
binding.fsync(fd, req);
10221022
};
10231023

10241024
fs.fsyncSync = function(fd) {
1025-
if (!isUint32(fd))
1026-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
1025+
validateFd(fd);
10271026
return binding.fsync(fd);
10281027
};
10291028

@@ -1089,8 +1088,7 @@ fs.readdirSync = function(path, options) {
10891088
};
10901089

10911090
fs.fstat = function(fd, callback) {
1092-
if (!isUint32(fd))
1093-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
1091+
validateFd(fd);
10941092
const req = new FSReqWrap();
10951093
req.oncomplete = makeStatsCallback(callback);
10961094
binding.fstat(fd, req);
@@ -1125,8 +1123,7 @@ fs.stat = function(path, callback) {
11251123
};
11261124

11271125
fs.fstatSync = function(fd) {
1128-
if (!isUint32(fd))
1129-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
1126+
validateFd(fd);
11301127
binding.fstat(fd);
11311128
return statsFromValues();
11321129
};
@@ -1336,8 +1333,7 @@ fs.unlinkSync = function(path) {
13361333

13371334
fs.fchmod = function(fd, mode, callback) {
13381335
mode = modeNum(mode);
1339-
if (!isUint32(fd))
1340-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
1336+
validateFd(fd);
13411337
if (!isUint32(mode))
13421338
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
13431339
if (mode < 0 || mode > 0o777)
@@ -1350,8 +1346,7 @@ fs.fchmod = function(fd, mode, callback) {
13501346

13511347
fs.fchmodSync = function(fd, mode) {
13521348
mode = modeNum(mode);
1353-
if (!isUint32(fd))
1354-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
1349+
validateFd(fd);
13551350
if (!isUint32(mode))
13561351
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
13571352
if (mode < 0 || mode > 0o777)
@@ -1448,8 +1443,7 @@ if (constants.O_SYMLINK !== undefined) {
14481443
}
14491444

14501445
fs.fchown = function(fd, uid, gid, callback) {
1451-
if (!isUint32(fd))
1452-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
1446+
validateFd(fd);
14531447
if (!isUint32(uid))
14541448
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'integer');
14551449
if (!isUint32(gid))
@@ -1461,8 +1455,7 @@ fs.fchown = function(fd, uid, gid, callback) {
14611455
};
14621456

14631457
fs.fchownSync = function(fd, uid, gid) {
1464-
if (!isUint32(fd))
1465-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
1458+
validateFd(fd);
14661459
if (!isUint32(uid))
14671460
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'integer');
14681461
if (!isUint32(gid))
@@ -1562,8 +1555,7 @@ fs.utimesSync = function(path, atime, mtime) {
15621555
};
15631556

15641557
fs.futimes = function(fd, atime, mtime, callback) {
1565-
if (!isUint32(fd))
1566-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
1558+
validateFd(fd);
15671559
atime = toUnixTimestamp(atime, 'atime');
15681560
mtime = toUnixTimestamp(mtime, 'mtime');
15691561
const req = new FSReqWrap();
@@ -1572,8 +1564,7 @@ fs.futimes = function(fd, atime, mtime, callback) {
15721564
};
15731565

15741566
fs.futimesSync = function(fd, atime, mtime) {
1575-
if (!isUint32(fd))
1576-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
1567+
validateFd(fd);
15771568
atime = toUnixTimestamp(atime, 'atime');
15781569
mtime = toUnixTimestamp(mtime, 'mtime');
15791570
binding.futimes(fd, atime, mtime);

0 commit comments

Comments
 (0)