summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDarshan Sen <raisinten@gmail.com>2021-01-24 19:43:05 +0530
committerMichaƫl Zasso <targos@protonmail.com>2021-02-02 10:47:39 +0100
commit6f54a14cda79c30b3ee8b0eb75b8e5b184aa1e8d (patch)
tree35a128d5116fb40488f96002c08ddf1f932c5c3d
parentdb38cf27c236dee8f4f112809c8cc9870bf32ac4 (diff)
downloadnode-new-6f54a14cda79c30b3ee8b0eb75b8e5b184aa1e8d.tar.gz
fs: add validatePosition and use in read and readSync
PR-URL: https://github.com/nodejs/node/pull/37051 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
-rw-r--r--lib/fs.js30
-rw-r--r--lib/internal/fs/utils.js20
2 files changed, 22 insertions, 28 deletions
diff --git a/lib/fs.js b/lib/fs.js
index 966016522f..d01e99c21b 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -75,7 +75,6 @@ const {
ERR_INVALID_ARG_VALUE,
ERR_INVALID_ARG_TYPE,
ERR_FEATURE_UNAVAILABLE_ON_PLATFORM,
- ERR_OUT_OF_RANGE,
},
hideStackFrames,
uvErrmapGet,
@@ -105,6 +104,7 @@ const {
validateOffsetLengthRead,
validateOffsetLengthWrite,
validatePath,
+ validatePosition,
validateRmOptions,
validateRmOptionsSync,
validateRmdirOptions,
@@ -550,19 +550,7 @@ function read(fd, buffer, offset, length, position, callback) {
if (position == null)
position = -1;
- if (typeof position === 'number') {
- validateInteger(position, 'position');
- } else if (typeof position === 'bigint') {
- if (!(position >= -(2n ** 63n) && position <= 2n ** 63n - 1n)) {
- throw new ERR_OUT_OF_RANGE('position',
- `>= ${-(2n ** 63n)} && <= ${2n ** 63n - 1n}`,
- position);
- }
- } else {
- throw new ERR_INVALID_ARG_TYPE('position',
- ['integer', 'bigint'],
- position);
- }
+ validatePosition(position, 'position');
function wrapper(err, bytesRead) {
// Retain a reference to buffer so that it can't be GC'ed too soon.
@@ -616,19 +604,7 @@ function readSync(fd, buffer, offset, length, position) {
if (position == null)
position = -1;
- if (typeof position === 'number') {
- validateInteger(position, 'position');
- } else if (typeof position === 'bigint') {
- if (!(position >= -(2n ** 63n) && position <= 2n ** 63n - 1n)) {
- throw new ERR_OUT_OF_RANGE('position',
- `>= ${-(2n ** 63n)} && <= ${2n ** 63n - 1n}`,
- position);
- }
- } else {
- throw new ERR_INVALID_ARG_TYPE('position',
- ['integer', 'bigint'],
- position);
- }
+ validatePosition(position, 'position');
const ctx = {};
const result = binding.read(fd, buffer, offset, length, position,
diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js
index 3b4ef8c423..6b50a60051 100644
--- a/lib/internal/fs/utils.js
+++ b/lib/internal/fs/utils.js
@@ -47,7 +47,8 @@ const {
validateAbortSignal,
validateBoolean,
validateInt32,
- validateUint32
+ validateInteger,
+ validateUint32,
} = require('internal/validators');
const pathModule = require('path');
const kType = Symbol('type');
@@ -818,6 +819,22 @@ const validateStringAfterArrayBufferView = hideStackFrames((buffer, name) => {
);
});
+const validatePosition = hideStackFrames((position, name) => {
+ if (typeof position === 'number') {
+ validateInteger(position, 'position');
+ } else if (typeof position === 'bigint') {
+ if (!(position >= -(2n ** 63n) && position <= 2n ** 63n - 1n)) {
+ throw new ERR_OUT_OF_RANGE('position',
+ `>= ${-(2n ** 63n)} && <= ${2n ** 63n - 1n}`,
+ position);
+ }
+ } else {
+ throw new ERR_INVALID_ARG_TYPE('position',
+ ['integer', 'bigint'],
+ position);
+ }
+});
+
module.exports = {
assertEncoding,
BigIntStats, // for testing
@@ -841,6 +858,7 @@ module.exports = {
validateOffsetLengthRead,
validateOffsetLengthWrite,
validatePath,
+ validatePosition,
validateRmOptions,
validateRmOptionsSync,
validateRmdirOptions,