From 6f54a14cda79c30b3ee8b0eb75b8e5b184aa1e8d Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sun, 24 Jan 2021 19:43:05 +0530 Subject: [PATCH] fs: add validatePosition and use in read and readSync PR-URL: https://github.com/nodejs/node/pull/37051 Reviewed-By: Antoine du Hamel Reviewed-By: Benjamin Gruenbaum --- lib/fs.js | 30 +++--------------------------- lib/internal/fs/utils.js | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 966016522f02a7..d01e99c21b0178 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 3b4ef8c423712e..6b50a60051634a 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,