Skip to content

Commit 8aec363

Browse files
maclover7joyeecheung
authored andcommitted
fs: extract out validateUint32 and validateLen functions
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 46e1d69 commit 8aec363

File tree

1 file changed

+54
-62
lines changed

1 file changed

+54
-62
lines changed

lib/fs.js

Lines changed: 54 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,14 @@ function validateBuffer(buffer) {
162162
}
163163
}
164164

165-
function validateFd(fd) {
165+
function validateLen(len) {
166166
let err;
167167

168-
if (!isUint32(fd))
169-
err = new errors.TypeError('ERR_INVALID_ARG_TYPE', 'fd', 'integer');
168+
if (!isInt32(len))
169+
err = new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'integer');
170170

171171
if (err !== undefined) {
172-
Error.captureStackTrace(err, validateFd);
172+
Error.captureStackTrace(err, validateLen);
173173
throw err;
174174
}
175175
}
@@ -222,6 +222,18 @@ function validatePath(path, propName) {
222222
}
223223
}
224224

225+
function validateUint32(value, propName) {
226+
let err;
227+
228+
if (!isUint32(value))
229+
err = new errors.TypeError('ERR_INVALID_ARG_TYPE', propName, 'integer');
230+
231+
if (err !== undefined) {
232+
Error.captureStackTrace(err, validateUint32);
233+
throw err;
234+
}
235+
}
236+
225237
// Special case of `makeCallback()` that is specific to async `*stat()` calls as
226238
// an optimization, since the data passed back to the callback needs to be
227239
// transformed anyway.
@@ -708,14 +720,14 @@ fs.readFileSync = function(path, options) {
708720
};
709721

710722
fs.close = function(fd, callback) {
711-
validateFd(fd);
723+
validateUint32(fd, 'fd');
712724
const req = new FSReqWrap();
713725
req.oncomplete = makeCallback(callback);
714726
binding.close(fd, req);
715727
};
716728

717729
fs.closeSync = function(fd) {
718-
validateFd(fd);
730+
validateUint32(fd, 'fd');
719731

720732
const ctx = {};
721733
binding.close(fd, undefined, ctx);
@@ -742,9 +754,7 @@ fs.open = function(path, flags, mode, callback_) {
742754
return;
743755
if (!nullCheck(path, callback)) return;
744756
validatePath(path);
745-
746-
if (!isUint32(mode))
747-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
757+
validateUint32(mode, 'mode');
748758

749759
const req = new FSReqWrap();
750760
req.oncomplete = callback;
@@ -760,16 +770,14 @@ fs.openSync = function(path, flags, mode) {
760770
handleError((path = getPathFromURL(path)));
761771
nullCheck(path);
762772
validatePath(path);
763-
764-
if (!isUint32(mode))
765-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
773+
validateUint32(mode, 'mode');
766774

767775
return binding.open(pathModule.toNamespacedPath(path),
768776
stringToFlags(flags), mode);
769777
};
770778

771779
fs.read = function(fd, buffer, offset, length, position, callback) {
772-
validateFd(fd);
780+
validateUint32(fd, 'fd');
773781
validateBuffer(buffer);
774782

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

803811
fs.readSync = function(fd, buffer, offset, length, position) {
804-
validateFd(fd);
812+
validateUint32(fd, 'fd');
805813
validateBuffer(buffer);
806814

807815
offset |= 0;
@@ -829,7 +837,7 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
829837
callback(err, written || 0, buffer);
830838
}
831839

832-
validateFd(fd);
840+
validateUint32(fd, 'fd');
833841

834842
const req = new FSReqWrap();
835843
req.oncomplete = wrapper;
@@ -869,7 +877,7 @@ Object.defineProperty(fs.write, internalUtil.customPromisifyArgs,
869877
// OR
870878
// fs.writeSync(fd, string[, position[, encoding]]);
871879
fs.writeSync = function(fd, buffer, offset, length, position) {
872-
validateFd(fd);
880+
validateUint32(fd, 'fd');
873881
if (isUint8Array(buffer)) {
874882
if (position === undefined)
875883
position = null;
@@ -968,19 +976,17 @@ fs.ftruncate = function(fd, len = 0, callback) {
968976
callback = len;
969977
len = 0;
970978
}
971-
validateFd(fd);
972-
if (!isInt32(len))
973-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'integer');
979+
validateUint32(fd, 'fd');
980+
validateLen(len);
974981
len = Math.max(0, len);
975982
const req = new FSReqWrap();
976983
req.oncomplete = makeCallback(callback);
977984
binding.ftruncate(fd, len, req);
978985
};
979986

980987
fs.ftruncateSync = function(fd, len = 0) {
981-
validateFd(fd);
982-
if (!isInt32(len))
983-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'integer');
988+
validateUint32(fd, 'fd');
989+
validateLen(len);
984990
len = Math.max(0, len);
985991
return binding.ftruncate(fd, len);
986992
};
@@ -1004,26 +1010,26 @@ fs.rmdirSync = function(path) {
10041010
};
10051011

10061012
fs.fdatasync = function(fd, callback) {
1007-
validateFd(fd);
1013+
validateUint32(fd, 'fd');
10081014
const req = new FSReqWrap();
10091015
req.oncomplete = makeCallback(callback);
10101016
binding.fdatasync(fd, req);
10111017
};
10121018

10131019
fs.fdatasyncSync = function(fd) {
1014-
validateFd(fd);
1020+
validateUint32(fd, 'fd');
10151021
return binding.fdatasync(fd);
10161022
};
10171023

10181024
fs.fsync = function(fd, callback) {
1019-
validateFd(fd);
1025+
validateUint32(fd, 'fd');
10201026
const req = new FSReqWrap();
10211027
req.oncomplete = makeCallback(callback);
10221028
binding.fsync(fd, req);
10231029
};
10241030

10251031
fs.fsyncSync = function(fd) {
1026-
validateFd(fd);
1032+
validateUint32(fd, 'fd');
10271033
return binding.fsync(fd);
10281034
};
10291035

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

10371043
validatePath(path);
10381044
mode = modeNum(mode, 0o777);
1039-
if (!isUint32(mode))
1040-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
1045+
validateUint32(mode, 'mode');
10411046

10421047
const req = new FSReqWrap();
10431048
req.oncomplete = callback;
@@ -1049,8 +1054,7 @@ fs.mkdirSync = function(path, mode) {
10491054
nullCheck(path);
10501055
validatePath(path);
10511056
mode = modeNum(mode, 0o777);
1052-
if (!isUint32(mode))
1053-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
1057+
validateUint32(mode, 'mode');
10541058
return binding.mkdir(pathModule.toNamespacedPath(path), mode);
10551059
};
10561060

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

10791083
fs.fstat = function(fd, callback) {
1080-
validateFd(fd);
1084+
validateUint32(fd, 'fd');
10811085
const req = new FSReqWrap();
10821086
req.oncomplete = makeStatsCallback(callback);
10831087
binding.fstat(fd, req);
@@ -1106,7 +1110,7 @@ fs.stat = function(path, callback) {
11061110
};
11071111

11081112
fs.fstatSync = function(fd) {
1109-
validateFd(fd);
1113+
validateUint32(fd, 'fd');
11101114
binding.fstat(fd);
11111115
return statsFromValues();
11121116
};
@@ -1273,9 +1277,8 @@ fs.unlinkSync = function(path) {
12731277

12741278
fs.fchmod = function(fd, mode, callback) {
12751279
mode = modeNum(mode);
1276-
validateFd(fd);
1277-
if (!isUint32(mode))
1278-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
1280+
validateUint32(fd, 'fd');
1281+
validateUint32(mode, 'mode');
12791282
if (mode < 0 || mode > 0o777)
12801283
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'mode');
12811284

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

12871290
fs.fchmodSync = function(fd, mode) {
12881291
mode = modeNum(mode);
1289-
validateFd(fd);
1290-
if (!isUint32(mode))
1291-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
1292+
validateUint32(fd, 'fd');
1293+
validateUint32(mode, 'mode');
12921294
if (mode < 0 || mode > 0o777)
12931295
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'mode');
12941296
return binding.fchmod(fd, mode);
@@ -1340,8 +1342,7 @@ fs.chmod = function(path, mode, callback) {
13401342

13411343
validatePath(path);
13421344
mode = modeNum(mode);
1343-
if (!isUint32(mode))
1344-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
1345+
validateUint32(mode, 'mode');
13451346

13461347
const req = new FSReqWrap();
13471348
req.oncomplete = callback;
@@ -1353,8 +1354,7 @@ fs.chmodSync = function(path, mode) {
13531354
nullCheck(path);
13541355
validatePath(path);
13551356
mode = modeNum(mode);
1356-
if (!isUint32(mode))
1357-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'integer');
1357+
validateUint32(mode, 'mode');
13581358
return binding.chmod(pathModule.toNamespacedPath(path), mode);
13591359
};
13601360

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

13791379
fs.fchown = function(fd, uid, gid, callback) {
1380-
validateFd(fd);
1381-
if (!isUint32(uid))
1382-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'integer');
1383-
if (!isUint32(gid))
1384-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'gid', 'integer');
1380+
validateUint32(fd, 'fd');
1381+
validateUint32(uid, 'uid');
1382+
validateUint32(gid, 'gid');
13851383

13861384
const req = new FSReqWrap();
13871385
req.oncomplete = makeCallback(callback);
13881386
binding.fchown(fd, uid, gid, req);
13891387
};
13901388

13911389
fs.fchownSync = function(fd, uid, gid) {
1392-
validateFd(fd);
1393-
if (!isUint32(uid))
1394-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'integer');
1395-
if (!isUint32(gid))
1396-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'gid', 'integer');
1390+
validateUint32(fd, 'fd');
1391+
validateUint32(uid, 'uid');
1392+
validateUint32(gid, 'gid');
13971393

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

14071403
validatePath(path);
1408-
if (!isUint32(uid))
1409-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'integer');
1410-
if (!isUint32(gid))
1411-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'gid', 'integer');
1404+
validateUint32(uid, 'uid');
1405+
validateUint32(gid, 'gid');
14121406

14131407
const req = new FSReqWrap();
14141408
req.oncomplete = callback;
@@ -1419,10 +1413,8 @@ fs.chownSync = function(path, uid, gid) {
14191413
handleError((path = getPathFromURL(path)));
14201414
nullCheck(path);
14211415
validatePath(path);
1422-
if (!isUint32(uid))
1423-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'uid', 'integer');
1424-
if (!isUint32(gid))
1425-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'gid', 'integer');
1416+
validateUint32(uid, 'uid');
1417+
validateUint32(gid, 'gid');
14261418
return binding.chown(pathModule.toNamespacedPath(path), uid, gid);
14271419
};
14281420

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

14791471
fs.futimes = function(fd, atime, mtime, callback) {
1480-
validateFd(fd);
1472+
validateUint32(fd, 'fd');
14811473
atime = toUnixTimestamp(atime, 'atime');
14821474
mtime = toUnixTimestamp(mtime, 'mtime');
14831475
const req = new FSReqWrap();
@@ -1486,7 +1478,7 @@ fs.futimes = function(fd, atime, mtime, callback) {
14861478
};
14871479

14881480
fs.futimesSync = function(fd, atime, mtime) {
1489-
validateFd(fd);
1481+
validateUint32(fd, 'fd');
14901482
atime = toUnixTimestamp(atime, 'atime');
14911483
mtime = toUnixTimestamp(mtime, 'mtime');
14921484
binding.futimes(fd, atime, mtime);

0 commit comments

Comments
 (0)