Skip to content

Commit

Permalink
fs: add validatePosition and use in read and readSync
Browse files Browse the repository at this point in the history
PR-URL: #37051
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
RaisinTen authored and targos committed Feb 2, 2021
1 parent db38cf2 commit 6f54a14
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
30 changes: 3 additions & 27 deletions lib/fs.js
Expand Up @@ -75,7 +75,6 @@ const {
ERR_INVALID_ARG_VALUE,
ERR_INVALID_ARG_TYPE,
ERR_FEATURE_UNAVAILABLE_ON_PLATFORM,
ERR_OUT_OF_RANGE,
},
hideStackFrames,
uvErrmapGet,
Expand Down Expand Up @@ -105,6 +104,7 @@ const {
validateOffsetLengthRead,
validateOffsetLengthWrite,
validatePath,
validatePosition,
validateRmOptions,
validateRmOptionsSync,
validateRmdirOptions,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 19 additions & 1 deletion lib/internal/fs/utils.js
Expand Up @@ -47,7 +47,8 @@ const {
validateAbortSignal,
validateBoolean,
validateInt32,
validateUint32
validateInteger,
validateUint32,
} = require('internal/validators');
const pathModule = require('path');
const kType = Symbol('type');
Expand Down Expand Up @@ -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
Expand All @@ -841,6 +858,7 @@ module.exports = {
validateOffsetLengthRead,
validateOffsetLengthWrite,
validatePath,
validatePosition,
validateRmOptions,
validateRmOptionsSync,
validateRmdirOptions,
Expand Down

0 comments on commit 6f54a14

Please sign in to comment.