@@ -162,14 +162,14 @@ function validateBuffer(buffer) {
162
162
}
163
163
}
164
164
165
- function validateFd ( fd ) {
165
+ function validateLen ( len ) {
166
166
let err ;
167
167
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' ) ;
170
170
171
171
if ( err !== undefined ) {
172
- Error . captureStackTrace ( err , validateFd ) ;
172
+ Error . captureStackTrace ( err , validateLen ) ;
173
173
throw err ;
174
174
}
175
175
}
@@ -222,6 +222,18 @@ function validatePath(path, propName) {
222
222
}
223
223
}
224
224
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
+
225
237
// Special case of `makeCallback()` that is specific to async `*stat()` calls as
226
238
// an optimization, since the data passed back to the callback needs to be
227
239
// transformed anyway.
@@ -708,14 +720,14 @@ fs.readFileSync = function(path, options) {
708
720
} ;
709
721
710
722
fs . close = function ( fd , callback ) {
711
- validateFd ( fd ) ;
723
+ validateUint32 ( fd , 'fd' ) ;
712
724
const req = new FSReqWrap ( ) ;
713
725
req . oncomplete = makeCallback ( callback ) ;
714
726
binding . close ( fd , req ) ;
715
727
} ;
716
728
717
729
fs . closeSync = function ( fd ) {
718
- validateFd ( fd ) ;
730
+ validateUint32 ( fd , 'fd' ) ;
719
731
720
732
const ctx = { } ;
721
733
binding . close ( fd , undefined , ctx ) ;
@@ -742,9 +754,7 @@ fs.open = function(path, flags, mode, callback_) {
742
754
return ;
743
755
if ( ! nullCheck ( path , callback ) ) return ;
744
756
validatePath ( path ) ;
745
-
746
- if ( ! isUint32 ( mode ) )
747
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'mode' , 'integer' ) ;
757
+ validateUint32 ( mode , 'mode' ) ;
748
758
749
759
const req = new FSReqWrap ( ) ;
750
760
req . oncomplete = callback ;
@@ -760,16 +770,14 @@ fs.openSync = function(path, flags, mode) {
760
770
handleError ( ( path = getPathFromURL ( path ) ) ) ;
761
771
nullCheck ( path ) ;
762
772
validatePath ( path ) ;
763
-
764
- if ( ! isUint32 ( mode ) )
765
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'mode' , 'integer' ) ;
773
+ validateUint32 ( mode , 'mode' ) ;
766
774
767
775
return binding . open ( pathModule . toNamespacedPath ( path ) ,
768
776
stringToFlags ( flags ) , mode ) ;
769
777
} ;
770
778
771
779
fs . read = function ( fd , buffer , offset , length , position , callback ) {
772
- validateFd ( fd ) ;
780
+ validateUint32 ( fd , 'fd' ) ;
773
781
validateBuffer ( buffer ) ;
774
782
775
783
offset |= 0 ;
@@ -801,7 +809,7 @@ Object.defineProperty(fs.read, internalUtil.customPromisifyArgs,
801
809
{ value : [ 'bytesRead' , 'buffer' ] , enumerable : false } ) ;
802
810
803
811
fs . readSync = function ( fd , buffer , offset , length , position ) {
804
- validateFd ( fd ) ;
812
+ validateUint32 ( fd , 'fd' ) ;
805
813
validateBuffer ( buffer ) ;
806
814
807
815
offset |= 0 ;
@@ -829,7 +837,7 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
829
837
callback ( err , written || 0 , buffer ) ;
830
838
}
831
839
832
- validateFd ( fd ) ;
840
+ validateUint32 ( fd , 'fd' ) ;
833
841
834
842
const req = new FSReqWrap ( ) ;
835
843
req . oncomplete = wrapper ;
@@ -869,7 +877,7 @@ Object.defineProperty(fs.write, internalUtil.customPromisifyArgs,
869
877
// OR
870
878
// fs.writeSync(fd, string[, position[, encoding]]);
871
879
fs . writeSync = function ( fd , buffer , offset , length , position ) {
872
- validateFd ( fd ) ;
880
+ validateUint32 ( fd , 'fd' ) ;
873
881
if ( isUint8Array ( buffer ) ) {
874
882
if ( position === undefined )
875
883
position = null ;
@@ -968,19 +976,17 @@ fs.ftruncate = function(fd, len = 0, callback) {
968
976
callback = len ;
969
977
len = 0 ;
970
978
}
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 ) ;
974
981
len = Math . max ( 0 , len ) ;
975
982
const req = new FSReqWrap ( ) ;
976
983
req . oncomplete = makeCallback ( callback ) ;
977
984
binding . ftruncate ( fd , len , req ) ;
978
985
} ;
979
986
980
987
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 ) ;
984
990
len = Math . max ( 0 , len ) ;
985
991
return binding . ftruncate ( fd , len ) ;
986
992
} ;
@@ -1004,26 +1010,26 @@ fs.rmdirSync = function(path) {
1004
1010
} ;
1005
1011
1006
1012
fs . fdatasync = function ( fd , callback ) {
1007
- validateFd ( fd ) ;
1013
+ validateUint32 ( fd , 'fd' ) ;
1008
1014
const req = new FSReqWrap ( ) ;
1009
1015
req . oncomplete = makeCallback ( callback ) ;
1010
1016
binding . fdatasync ( fd , req ) ;
1011
1017
} ;
1012
1018
1013
1019
fs . fdatasyncSync = function ( fd ) {
1014
- validateFd ( fd ) ;
1020
+ validateUint32 ( fd , 'fd' ) ;
1015
1021
return binding . fdatasync ( fd ) ;
1016
1022
} ;
1017
1023
1018
1024
fs . fsync = function ( fd , callback ) {
1019
- validateFd ( fd ) ;
1025
+ validateUint32 ( fd , 'fd' ) ;
1020
1026
const req = new FSReqWrap ( ) ;
1021
1027
req . oncomplete = makeCallback ( callback ) ;
1022
1028
binding . fsync ( fd , req ) ;
1023
1029
} ;
1024
1030
1025
1031
fs . fsyncSync = function ( fd ) {
1026
- validateFd ( fd ) ;
1032
+ validateUint32 ( fd , 'fd' ) ;
1027
1033
return binding . fsync ( fd ) ;
1028
1034
} ;
1029
1035
@@ -1036,8 +1042,7 @@ fs.mkdir = function(path, mode, callback) {
1036
1042
1037
1043
validatePath ( path ) ;
1038
1044
mode = modeNum ( mode , 0o777 ) ;
1039
- if ( ! isUint32 ( mode ) )
1040
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'mode' , 'integer' ) ;
1045
+ validateUint32 ( mode , 'mode' ) ;
1041
1046
1042
1047
const req = new FSReqWrap ( ) ;
1043
1048
req . oncomplete = callback ;
@@ -1049,8 +1054,7 @@ fs.mkdirSync = function(path, mode) {
1049
1054
nullCheck ( path ) ;
1050
1055
validatePath ( path ) ;
1051
1056
mode = modeNum ( mode , 0o777 ) ;
1052
- if ( ! isUint32 ( mode ) )
1053
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'mode' , 'integer' ) ;
1057
+ validateUint32 ( mode , 'mode' ) ;
1054
1058
return binding . mkdir ( pathModule . toNamespacedPath ( path ) , mode ) ;
1055
1059
} ;
1056
1060
@@ -1077,7 +1081,7 @@ fs.readdirSync = function(path, options) {
1077
1081
} ;
1078
1082
1079
1083
fs . fstat = function ( fd , callback ) {
1080
- validateFd ( fd ) ;
1084
+ validateUint32 ( fd , 'fd' ) ;
1081
1085
const req = new FSReqWrap ( ) ;
1082
1086
req . oncomplete = makeStatsCallback ( callback ) ;
1083
1087
binding . fstat ( fd , req ) ;
@@ -1106,7 +1110,7 @@ fs.stat = function(path, callback) {
1106
1110
} ;
1107
1111
1108
1112
fs . fstatSync = function ( fd ) {
1109
- validateFd ( fd ) ;
1113
+ validateUint32 ( fd , 'fd' ) ;
1110
1114
binding . fstat ( fd ) ;
1111
1115
return statsFromValues ( ) ;
1112
1116
} ;
@@ -1273,9 +1277,8 @@ fs.unlinkSync = function(path) {
1273
1277
1274
1278
fs . fchmod = function ( fd , mode , callback ) {
1275
1279
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' ) ;
1279
1282
if ( mode < 0 || mode > 0o777 )
1280
1283
throw new errors . RangeError ( 'ERR_OUT_OF_RANGE' , 'mode' ) ;
1281
1284
@@ -1286,9 +1289,8 @@ fs.fchmod = function(fd, mode, callback) {
1286
1289
1287
1290
fs . fchmodSync = function ( fd , mode ) {
1288
1291
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' ) ;
1292
1294
if ( mode < 0 || mode > 0o777 )
1293
1295
throw new errors . RangeError ( 'ERR_OUT_OF_RANGE' , 'mode' ) ;
1294
1296
return binding . fchmod ( fd , mode ) ;
@@ -1340,8 +1342,7 @@ fs.chmod = function(path, mode, callback) {
1340
1342
1341
1343
validatePath ( path ) ;
1342
1344
mode = modeNum ( mode ) ;
1343
- if ( ! isUint32 ( mode ) )
1344
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'mode' , 'integer' ) ;
1345
+ validateUint32 ( mode , 'mode' ) ;
1345
1346
1346
1347
const req = new FSReqWrap ( ) ;
1347
1348
req . oncomplete = callback ;
@@ -1353,8 +1354,7 @@ fs.chmodSync = function(path, mode) {
1353
1354
nullCheck ( path ) ;
1354
1355
validatePath ( path ) ;
1355
1356
mode = modeNum ( mode ) ;
1356
- if ( ! isUint32 ( mode ) )
1357
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'mode' , 'integer' ) ;
1357
+ validateUint32 ( mode , 'mode' ) ;
1358
1358
return binding . chmod ( pathModule . toNamespacedPath ( path ) , mode ) ;
1359
1359
} ;
1360
1360
@@ -1377,23 +1377,19 @@ if (constants.O_SYMLINK !== undefined) {
1377
1377
}
1378
1378
1379
1379
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' ) ;
1385
1383
1386
1384
const req = new FSReqWrap ( ) ;
1387
1385
req . oncomplete = makeCallback ( callback ) ;
1388
1386
binding . fchown ( fd , uid , gid , req ) ;
1389
1387
} ;
1390
1388
1391
1389
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' ) ;
1397
1393
1398
1394
return binding . fchown ( fd , uid , gid ) ;
1399
1395
} ;
@@ -1405,10 +1401,8 @@ fs.chown = function(path, uid, gid, callback) {
1405
1401
if ( ! nullCheck ( path , callback ) ) return ;
1406
1402
1407
1403
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' ) ;
1412
1406
1413
1407
const req = new FSReqWrap ( ) ;
1414
1408
req . oncomplete = callback ;
@@ -1419,10 +1413,8 @@ fs.chownSync = function(path, uid, gid) {
1419
1413
handleError ( ( path = getPathFromURL ( path ) ) ) ;
1420
1414
nullCheck ( path ) ;
1421
1415
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' ) ;
1426
1418
return binding . chown ( pathModule . toNamespacedPath ( path ) , uid , gid ) ;
1427
1419
} ;
1428
1420
@@ -1477,7 +1469,7 @@ fs.utimesSync = function(path, atime, mtime) {
1477
1469
} ;
1478
1470
1479
1471
fs . futimes = function ( fd , atime , mtime , callback ) {
1480
- validateFd ( fd ) ;
1472
+ validateUint32 ( fd , 'fd' ) ;
1481
1473
atime = toUnixTimestamp ( atime , 'atime' ) ;
1482
1474
mtime = toUnixTimestamp ( mtime , 'mtime' ) ;
1483
1475
const req = new FSReqWrap ( ) ;
@@ -1486,7 +1478,7 @@ fs.futimes = function(fd, atime, mtime, callback) {
1486
1478
} ;
1487
1479
1488
1480
fs . futimesSync = function ( fd , atime , mtime ) {
1489
- validateFd ( fd ) ;
1481
+ validateUint32 ( fd , 'fd' ) ;
1490
1482
atime = toUnixTimestamp ( atime , 'atime' ) ;
1491
1483
mtime = toUnixTimestamp ( mtime , 'mtime' ) ;
1492
1484
binding . futimes ( fd , atime , mtime ) ;
0 commit comments