lib: rename validateSafeInteger to validateInteger
validateInteger() was renamed to validateSafeInteger() in https://github.com/nodejs/node/pull/26572. However, this function also works with unsafe integers. This commit restores the old name, and adds some basic tests. PR-URL: https://github.com/nodejs/node/pull/29184 Refs: https://github.com/nodejs/node/pull/26572 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
parent
37321a9e11
commit
3238232fc4
16
lib/fs.js
16
lib/fs.js
@ -85,7 +85,7 @@ const {
|
|||||||
isUint32,
|
isUint32,
|
||||||
parseMode,
|
parseMode,
|
||||||
validateBuffer,
|
validateBuffer,
|
||||||
validateSafeInteger,
|
validateInteger,
|
||||||
validateInt32,
|
validateInt32,
|
||||||
validateUint32
|
validateUint32
|
||||||
} = require('internal/validators');
|
} = require('internal/validators');
|
||||||
@ -469,7 +469,7 @@ function read(fd, buffer, offset, length, position, callback) {
|
|||||||
if (offset == null) {
|
if (offset == null) {
|
||||||
offset = 0;
|
offset = 0;
|
||||||
} else {
|
} else {
|
||||||
validateSafeInteger(offset, 'offset');
|
validateInteger(offset, 'offset');
|
||||||
}
|
}
|
||||||
|
|
||||||
length |= 0;
|
length |= 0;
|
||||||
@ -511,7 +511,7 @@ function readSync(fd, buffer, offset, length, position) {
|
|||||||
if (offset == null) {
|
if (offset == null) {
|
||||||
offset = 0;
|
offset = 0;
|
||||||
} else {
|
} else {
|
||||||
validateSafeInteger(offset, 'offset');
|
validateInteger(offset, 'offset');
|
||||||
}
|
}
|
||||||
|
|
||||||
length |= 0;
|
length |= 0;
|
||||||
@ -557,7 +557,7 @@ function write(fd, buffer, offset, length, position, callback) {
|
|||||||
if (offset == null || typeof offset === 'function') {
|
if (offset == null || typeof offset === 'function') {
|
||||||
offset = 0;
|
offset = 0;
|
||||||
} else {
|
} else {
|
||||||
validateSafeInteger(offset, 'offset');
|
validateInteger(offset, 'offset');
|
||||||
}
|
}
|
||||||
if (typeof length !== 'number')
|
if (typeof length !== 'number')
|
||||||
length = buffer.length - offset;
|
length = buffer.length - offset;
|
||||||
@ -599,7 +599,7 @@ function writeSync(fd, buffer, offset, length, position) {
|
|||||||
if (offset == null) {
|
if (offset == null) {
|
||||||
offset = 0;
|
offset = 0;
|
||||||
} else {
|
} else {
|
||||||
validateSafeInteger(offset, 'offset');
|
validateInteger(offset, 'offset');
|
||||||
}
|
}
|
||||||
if (typeof length !== 'number')
|
if (typeof length !== 'number')
|
||||||
length = buffer.byteLength - offset;
|
length = buffer.byteLength - offset;
|
||||||
@ -698,7 +698,7 @@ function truncate(path, len, callback) {
|
|||||||
len = 0;
|
len = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
validateSafeInteger(len, 'len');
|
validateInteger(len, 'len');
|
||||||
callback = maybeCallback(callback);
|
callback = maybeCallback(callback);
|
||||||
fs.open(path, 'r+', (er, fd) => {
|
fs.open(path, 'r+', (er, fd) => {
|
||||||
if (er) return callback(er);
|
if (er) return callback(er);
|
||||||
@ -739,7 +739,7 @@ function ftruncate(fd, len = 0, callback) {
|
|||||||
len = 0;
|
len = 0;
|
||||||
}
|
}
|
||||||
validateInt32(fd, 'fd', 0);
|
validateInt32(fd, 'fd', 0);
|
||||||
validateSafeInteger(len, 'len');
|
validateInteger(len, 'len');
|
||||||
len = Math.max(0, len);
|
len = Math.max(0, len);
|
||||||
const req = new FSReqCallback();
|
const req = new FSReqCallback();
|
||||||
req.oncomplete = makeCallback(callback);
|
req.oncomplete = makeCallback(callback);
|
||||||
@ -748,7 +748,7 @@ function ftruncate(fd, len = 0, callback) {
|
|||||||
|
|
||||||
function ftruncateSync(fd, len = 0) {
|
function ftruncateSync(fd, len = 0) {
|
||||||
validateInt32(fd, 'fd', 0);
|
validateInt32(fd, 'fd', 0);
|
||||||
validateSafeInteger(len, 'len');
|
validateInteger(len, 'len');
|
||||||
len = Math.max(0, len);
|
len = Math.max(0, len);
|
||||||
const ctx = {};
|
const ctx = {};
|
||||||
binding.ftruncate(fd, len, undefined, ctx);
|
binding.ftruncate(fd, len, undefined, ctx);
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
const { AsyncWrap, Providers } = internalBinding('async_wrap');
|
const { AsyncWrap, Providers } = internalBinding('async_wrap');
|
||||||
const { Buffer } = require('buffer');
|
const { Buffer } = require('buffer');
|
||||||
const { scrypt: _scrypt } = internalBinding('crypto');
|
const { scrypt: _scrypt } = internalBinding('crypto');
|
||||||
const { validateSafeInteger, validateUint32 } = require('internal/validators');
|
const { validateInteger, validateUint32 } = require('internal/validators');
|
||||||
const {
|
const {
|
||||||
ERR_CRYPTO_SCRYPT_INVALID_PARAMETER,
|
ERR_CRYPTO_SCRYPT_INVALID_PARAMETER,
|
||||||
ERR_CRYPTO_SCRYPT_NOT_SUPPORTED,
|
ERR_CRYPTO_SCRYPT_NOT_SUPPORTED,
|
||||||
@ -108,7 +108,7 @@ function check(password, salt, keylen, options) {
|
|||||||
}
|
}
|
||||||
if (options.maxmem !== undefined) {
|
if (options.maxmem !== undefined) {
|
||||||
maxmem = options.maxmem;
|
maxmem = options.maxmem;
|
||||||
validateSafeInteger(maxmem, 'maxmem', 0);
|
validateInteger(maxmem, 'maxmem', 0);
|
||||||
}
|
}
|
||||||
if (N === 0) N = defaults.N;
|
if (N === 0) N = defaults.N;
|
||||||
if (r === 0) r = defaults.r;
|
if (r === 0) r = defaults.r;
|
||||||
|
@ -36,7 +36,7 @@ const {
|
|||||||
const {
|
const {
|
||||||
parseMode,
|
parseMode,
|
||||||
validateBuffer,
|
validateBuffer,
|
||||||
validateSafeInteger,
|
validateInteger,
|
||||||
validateUint32
|
validateUint32
|
||||||
} = require('internal/validators');
|
} = require('internal/validators');
|
||||||
const pathModule = require('path');
|
const pathModule = require('path');
|
||||||
@ -209,7 +209,7 @@ async function read(handle, buffer, offset, length, position) {
|
|||||||
if (offset == null) {
|
if (offset == null) {
|
||||||
offset = 0;
|
offset = 0;
|
||||||
} else {
|
} else {
|
||||||
validateSafeInteger(offset, 'offset');
|
validateInteger(offset, 'offset');
|
||||||
}
|
}
|
||||||
|
|
||||||
length |= 0;
|
length |= 0;
|
||||||
@ -243,7 +243,7 @@ async function write(handle, buffer, offset, length, position) {
|
|||||||
if (offset == null) {
|
if (offset == null) {
|
||||||
offset = 0;
|
offset = 0;
|
||||||
} else {
|
} else {
|
||||||
validateSafeInteger(offset, 'offset');
|
validateInteger(offset, 'offset');
|
||||||
}
|
}
|
||||||
if (typeof length !== 'number')
|
if (typeof length !== 'number')
|
||||||
length = buffer.length - offset;
|
length = buffer.length - offset;
|
||||||
@ -278,7 +278,7 @@ async function truncate(path, len = 0) {
|
|||||||
|
|
||||||
async function ftruncate(handle, len = 0) {
|
async function ftruncate(handle, len = 0) {
|
||||||
validateFileHandle(handle);
|
validateFileHandle(handle);
|
||||||
validateSafeInteger(len, 'len');
|
validateInteger(len, 'len');
|
||||||
len = Math.max(0, len);
|
len = Math.max(0, len);
|
||||||
return binding.ftruncate(handle.fd, len, kUsePromises);
|
return binding.ftruncate(handle.fd, len, kUsePromises);
|
||||||
}
|
}
|
||||||
|
@ -62,7 +62,7 @@ function parseMode(value, name, def) {
|
|||||||
throw new ERR_INVALID_ARG_VALUE(name, value, modeDesc);
|
throw new ERR_INVALID_ARG_VALUE(name, value, modeDesc);
|
||||||
}
|
}
|
||||||
|
|
||||||
const validateSafeInteger = hideStackFrames(
|
const validateInteger = hideStackFrames(
|
||||||
(value, name, min = MIN_SAFE_INTEGER, max = MAX_SAFE_INTEGER) => {
|
(value, name, min = MIN_SAFE_INTEGER, max = MAX_SAFE_INTEGER) => {
|
||||||
if (typeof value !== 'number')
|
if (typeof value !== 'number')
|
||||||
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
|
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
|
||||||
@ -161,7 +161,7 @@ module.exports = {
|
|||||||
parseMode,
|
parseMode,
|
||||||
validateBuffer,
|
validateBuffer,
|
||||||
validateEncoding,
|
validateEncoding,
|
||||||
validateSafeInteger,
|
validateInteger,
|
||||||
validateInt32,
|
validateInt32,
|
||||||
validateUint32,
|
validateUint32,
|
||||||
validateString,
|
validateString,
|
||||||
|
25
test/parallel/test-validators.js
Normal file
25
test/parallel/test-validators.js
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
// Flags: --expose-internals
|
||||||
|
'use strict';
|
||||||
|
const common = require('../common');
|
||||||
|
const {
|
||||||
|
validateInteger
|
||||||
|
} = require('internal/validators');
|
||||||
|
const { MAX_SAFE_INTEGER, MIN_SAFE_INTEGER } = Number;
|
||||||
|
const outOfRangeError = {
|
||||||
|
code: 'ERR_OUT_OF_RANGE',
|
||||||
|
type: RangeError
|
||||||
|
};
|
||||||
|
|
||||||
|
// validateInteger() defaults to validating safe integers.
|
||||||
|
validateInteger(MAX_SAFE_INTEGER, 'foo');
|
||||||
|
validateInteger(MIN_SAFE_INTEGER, 'foo');
|
||||||
|
common.expectsError(() => {
|
||||||
|
validateInteger(MAX_SAFE_INTEGER + 1, 'foo');
|
||||||
|
}, outOfRangeError);
|
||||||
|
common.expectsError(() => {
|
||||||
|
validateInteger(MIN_SAFE_INTEGER - 1, 'foo');
|
||||||
|
}, outOfRangeError);
|
||||||
|
|
||||||
|
// validateInteger() works with unsafe integers.
|
||||||
|
validateInteger(MAX_SAFE_INTEGER + 1, 'foo', 0, MAX_SAFE_INTEGER + 1);
|
||||||
|
validateInteger(MIN_SAFE_INTEGER - 1, 'foo', MIN_SAFE_INTEGER - 1);
|
Loading…
x
Reference in New Issue
Block a user