Skip to content

Commit f2c9e5a

Browse files
targosMylesBorins
authored andcommitted
lib: introduce internal/validators
Create a file to centralize argument validators that are used in multiple internal modules. Move validateInt32 and validateUint32 to this file. PR-URL: #21149 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Myles Borins <myles.borins@gmail.com>
1 parent 7c0c61b commit f2c9e5a

5 files changed

Lines changed: 76 additions & 56 deletions

File tree

‎lib/fs.js‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ const internalUtil = require('internal/util');
6565
const{
6666
copyObject,
6767
getOptions,
68-
isUint32,
6968
modeNum,
7069
nullCheck,
7170
preprocessSymlinkDestination,
@@ -76,16 +75,19 @@ const {
7675
stringToSymlinkType,
7776
toUnixTimestamp,
7877
validateBuffer,
79-
validateLen,
8078
validateOffsetLengthRead,
8179
validateOffsetLengthWrite,
82-
validatePath,
83-
validateUint32
80+
validatePath
8481
}=internalFS;
8582
const{
8683
CHAR_FORWARD_SLASH,
8784
CHAR_BACKWARD_SLASH,
8885
}=require('internal/constants');
86+
const{
87+
isUint32,
88+
validateInt32,
89+
validateUint32
90+
}=require('internal/validators');
8991

9092
// Lazy loaded
9193
letpromises;
@@ -787,8 +789,8 @@ fs.ftruncate = function(fd, len = 0, callback) {
787789
// TODO(BridgeAR): This does not seem right.
788790
// There does not seem to be any validation before and if there is any, it
789791
// should work similar to validateUint32 or not have a upper cap at all.
790-
// This applies to all usage of `validateLen`.
791-
validateLen(len);
792+
// This applies to all usage of `validateInt32(len, 'len')`.
793+
validateInt32(len,'len');
792794
len=Math.max(0,len);
793795
constreq=newFSReqWrap();
794796
req.oncomplete=makeCallback(callback);
@@ -797,7 +799,7 @@ fs.ftruncate = function(fd, len = 0, callback) {
797799

798800
fs.ftruncateSync=function(fd,len=0){
799801
validateUint32(fd,'fd');
800-
validateLen(len);
802+
validateInt32(len,'len');
801803
len=Math.max(0,len);
802804
constctx={};
803805
binding.ftruncate(fd,len,undefined,ctx);

‎lib/internal/fs/promises.js‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,22 @@ const {
2121
copyObject,
2222
getOptions,
2323
getStatsFromBinding,
24-
isUint32,
2524
modeNum,
2625
nullCheck,
2726
preprocessSymlinkDestination,
2827
stringToFlags,
2928
stringToSymlinkType,
3029
toUnixTimestamp,
3130
validateBuffer,
32-
validateLen,
3331
validateOffsetLengthRead,
3432
validateOffsetLengthWrite,
35-
validatePath,
36-
validateUint32
33+
validatePath
3734
}=require('internal/fs/utils');
35+
const{
36+
isUint32,
37+
validateInt32,
38+
validateUint32
39+
}=require('internal/validators');
3840
constpathModule=require('path');
3941

4042
constkHandle=Symbol('handle');
@@ -263,7 +265,7 @@ async function truncate(path, len = 0) {
263265

264266
asyncfunctionftruncate(handle,len=0){
265267
validateFileHandle(handle);
266-
validateLen(len);
268+
validateInt32(len,'len');
267269
len=Math.max(0,len);
268270
returnbinding.ftruncate(handle.fd,len,kUsePromises);
269271
}

‎lib/internal/fs/utils.js‎

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ function getOptions(options, defaultOptions) {
7070
returnoptions;
7171
}
7272

73-
functionisInt32(n){returnn===(n|0);}
74-
functionisUint32(n){returnn===(n>>>0);}
75-
7673
functionmodeNum(m,def){
7774
if(typeofm==='number')
7875
returnm;
@@ -341,26 +338,6 @@ function validateBuffer(buffer) {
341338
}
342339
}
343340

344-
functionvalidateLen(len){
345-
leterr;
346-
347-
if(!isInt32(len)){
348-
if(typeoflen!=='number'){
349-
err=newERR_INVALID_ARG_TYPE('len','number',len);
350-
}elseif(!Number.isInteger(len)){
351-
err=newERR_OUT_OF_RANGE('len','an integer',len);
352-
}else{
353-
// 2 ** 31 === 2147483648
354-
err=newERR_OUT_OF_RANGE('len','> -2147483649 && < 2147483648',len);
355-
}
356-
}
357-
358-
if(err!==undefined){
359-
Error.captureStackTrace(err,validateLen);
360-
throwerr;
361-
}
362-
}
363-
364341
functionvalidateOffsetLengthRead(offset,length,bufferLength){
365342
leterr;
366343

@@ -410,28 +387,10 @@ function validatePath(path, propName = 'path') {
410387
}
411388
}
412389

413-
functionvalidateUint32(value,propName){
414-
if(!isUint32(value)){
415-
leterr;
416-
if(typeofvalue!=='number'){
417-
err=newERR_INVALID_ARG_TYPE(propName,'number',value);
418-
}elseif(!Number.isInteger(value)){
419-
err=newERR_OUT_OF_RANGE(propName,'an integer',value);
420-
}else{
421-
// 2 ** 32 === 4294967296
422-
err=newERR_OUT_OF_RANGE(propName,'>= 0 && < 4294967296',value);
423-
}
424-
Error.captureStackTrace(err,validateUint32);
425-
throwerr;
426-
}
427-
}
428-
429390
module.exports={
430391
assertEncoding,
431392
copyObject,
432393
getOptions,
433-
isInt32,
434-
isUint32,
435394
modeNum,
436395
nullCheck,
437396
preprocessSymlinkDestination,
@@ -443,9 +402,7 @@ module.exports = {
443402
SyncWriteStream,
444403
toUnixTimestamp,
445404
validateBuffer,
446-
validateLen,
447405
validateOffsetLengthRead,
448406
validateOffsetLengthWrite,
449-
validatePath,
450-
validateUint32
407+
validatePath
451408
};

‎lib/internal/validators.js‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
3+
const{
4+
ERR_INVALID_ARG_TYPE,
5+
ERR_OUT_OF_RANGE
6+
}=require('internal/errors').codes;
7+
8+
functionisInt32(value){
9+
returnvalue===(value|0);
10+
}
11+
12+
functionisUint32(value){
13+
returnvalue===(value>>>0);
14+
}
15+
16+
functionvalidateInt32(value,name){
17+
if(!isInt32(value)){
18+
leterr;
19+
if(typeofvalue!=='number'){
20+
err=newERR_INVALID_ARG_TYPE(name,'number',value);
21+
}elseif(!Number.isInteger(value)){
22+
err=newERR_OUT_OF_RANGE(name,'an integer',value);
23+
}else{
24+
// 2 ** 31 === 2147483648
25+
err=newERR_OUT_OF_RANGE(name,'> -2147483649 && < 2147483648',value);
26+
}
27+
Error.captureStackTrace(err,validateInt32);
28+
throwerr;
29+
}
30+
}
31+
32+
functionvalidateUint32(value,name,positive){
33+
if(!isUint32(value)){
34+
leterr;
35+
if(typeofvalue!=='number'){
36+
err=newERR_INVALID_ARG_TYPE(name,'number',value);
37+
}elseif(!Number.isInteger(value)){
38+
err=newERR_OUT_OF_RANGE(name,'an integer',value);
39+
}else{
40+
constmin=positive ? 1 : 0;
41+
// 2 ** 32 === 4294967296
42+
err=newERR_OUT_OF_RANGE(name,`>= ${min} && < 4294967296`,value);
43+
}
44+
Error.captureStackTrace(err,validateUint32);
45+
throwerr;
46+
}elseif(positive&&value===0){
47+
consterr=newERR_OUT_OF_RANGE(name,'>= 1 && < 4294967296',value);
48+
Error.captureStackTrace(err,validateUint32);
49+
throwerr;
50+
}
51+
}
52+
53+
module.exports={
54+
isInt32,
55+
isUint32,
56+
validateInt32,
57+
validateUint32
58+
};

‎node.gyp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
'lib/internal/http2/util.js',
148148
'lib/internal/v8_prof_polyfill.js',
149149
'lib/internal/v8_prof_processor.js',
150+
'lib/internal/validators.js',
150151
'lib/internal/stream_base_commons.js',
151152
'lib/internal/vm/module.js',
152153
'lib/internal/streams/lazy_transform.js',

0 commit comments

Comments
 (0)