Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f2c9e5a

Browse filesBrowse 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
Copy full SHA for f2c9e5a

File tree

Expand file treeCollapse file tree

5 files changed

+76
-56
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+76
-56
lines changed
Open diff view settings
Collapse file

‎lib/fs.js‎

Copy file name to clipboardExpand all lines: lib/fs.js
+9-7Lines 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
let promises;
@@ -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
const req = new FSReqWrap();
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
const ctx = {};
803805
binding.ftruncate(fd, len, undefined, ctx);
Collapse file

‎lib/internal/fs/promises.js‎

Copy file name to clipboardExpand all lines: lib/internal/fs/promises.js
+7-5Lines 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
const pathModule = require('path');
3941

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

264266
async function ftruncate(handle, len = 0) {
265267
validateFileHandle(handle);
266-
validateLen(len);
268+
validateInt32(len, 'len');
267269
len = Math.max(0, len);
268270
return binding.ftruncate(handle.fd, len, kUsePromises);
269271
}
Collapse file

‎lib/internal/fs/utils.js‎

Copy file name to clipboardExpand all lines: lib/internal/fs/utils.js
+1-44Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ function getOptions(options, defaultOptions) {
7070
return options;
7171
}
7272

73-
function isInt32(n) { return n === (n | 0); }
74-
function isUint32(n) { return n === (n >>> 0); }
75-
7673
function modeNum(m, def) {
7774
if (typeof m === 'number')
7875
return m;
@@ -341,26 +338,6 @@ function validateBuffer(buffer) {
341338
}
342339
}
343340

344-
function validateLen(len) {
345-
let err;
346-
347-
if (!isInt32(len)) {
348-
if (typeof len !== 'number') {
349-
err = new ERR_INVALID_ARG_TYPE('len', 'number', len);
350-
} else if (!Number.isInteger(len)) {
351-
err = new ERR_OUT_OF_RANGE('len', 'an integer', len);
352-
} else {
353-
// 2 ** 31 === 2147483648
354-
err = new ERR_OUT_OF_RANGE('len', '> -2147483649 && < 2147483648', len);
355-
}
356-
}
357-
358-
if (err !== undefined) {
359-
Error.captureStackTrace(err, validateLen);
360-
throw err;
361-
}
362-
}
363-
364341
function validateOffsetLengthRead(offset, length, bufferLength) {
365342
let err;
366343

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

413-
function validateUint32(value, propName) {
414-
if (!isUint32(value)) {
415-
let err;
416-
if (typeof value !== 'number') {
417-
err = new ERR_INVALID_ARG_TYPE(propName, 'number', value);
418-
} else if (!Number.isInteger(value)) {
419-
err = new ERR_OUT_OF_RANGE(propName, 'an integer', value);
420-
} else {
421-
// 2 ** 32 === 4294967296
422-
err = new ERR_OUT_OF_RANGE(propName, '>= 0 && < 4294967296', value);
423-
}
424-
Error.captureStackTrace(err, validateUint32);
425-
throw err;
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
};
Collapse file

‎lib/internal/validators.js‎

Copy file name to clipboard
+58Lines 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+
function isInt32(value) {
9+
return value === (value | 0);
10+
}
11+
12+
function isUint32(value) {
13+
return value === (value >>> 0);
14+
}
15+
16+
function validateInt32(value, name) {
17+
if (!isInt32(value)) {
18+
let err;
19+
if (typeof value !== 'number') {
20+
err = new ERR_INVALID_ARG_TYPE(name, 'number', value);
21+
} else if (!Number.isInteger(value)) {
22+
err = new ERR_OUT_OF_RANGE(name, 'an integer', value);
23+
} else {
24+
// 2 ** 31 === 2147483648
25+
err = new ERR_OUT_OF_RANGE(name, '> -2147483649 && < 2147483648', value);
26+
}
27+
Error.captureStackTrace(err, validateInt32);
28+
throw err;
29+
}
30+
}
31+
32+
function validateUint32(value, name, positive) {
33+
if (!isUint32(value)) {
34+
let err;
35+
if (typeof value !== 'number') {
36+
err = new ERR_INVALID_ARG_TYPE(name, 'number', value);
37+
} else if (!Number.isInteger(value)) {
38+
err = new ERR_OUT_OF_RANGE(name, 'an integer', value);
39+
} else {
40+
const min = positive ? 1 : 0;
41+
// 2 ** 32 === 4294967296
42+
err = new ERR_OUT_OF_RANGE(name, `>= ${min} && < 4294967296`, value);
43+
}
44+
Error.captureStackTrace(err, validateUint32);
45+
throw err;
46+
} else if (positive && value === 0) {
47+
const err = new ERR_OUT_OF_RANGE(name, '>= 1 && < 4294967296', value);
48+
Error.captureStackTrace(err, validateUint32);
49+
throw err;
50+
}
51+
}
52+
53+
module.exports = {
54+
isInt32,
55+
isUint32,
56+
validateInt32,
57+
validateUint32
58+
};
Collapse file

‎node.gyp‎

Copy file name to clipboardExpand all lines: node.gyp
+1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.