From 0361b715dbce3816c5c1d5308029b8dda320085c Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sun, 24 Jan 2021 19:43:05 +0530 Subject: [PATCH 1/2] fs: add validatePosition and use in read and readSync --- lib/fs.js | 29 +++-------------------------- lib/internal/fs/utils.js | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 1bde6c81de5404..672524d0f29e9f 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -105,6 +105,7 @@ const { validateOffsetLengthRead, validateOffsetLengthWrite, validatePath, + validatePosition, validateRmOptions, validateRmOptionsSync, validateRmdirOptions, @@ -549,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. @@ -615,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 2353fd8c3cbcf2..3973fc3cc6239a 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'); @@ -825,6 +826,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 @@ -848,6 +865,7 @@ module.exports = { validateOffsetLengthRead, validateOffsetLengthWrite, validatePath, + validatePosition, validateRmOptions, validateRmOptionsSync, validateRmdirOptions, From 20ece4de7b072d05e5ae141019d147d6d6a3d88b Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sun, 24 Jan 2021 19:50:57 +0530 Subject: [PATCH 2/2] fixup! fs: add validatePosition and use in read and readSync --- lib/fs.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/fs.js b/lib/fs.js index 672524d0f29e9f..fd3352dca942db 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,