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 Aug 8, 2021
1 parent 3b25cbe commit dc8b210
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
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_CALLBACK,
ERR_FEATURE_UNAVAILABLE_ON_PLATFORM,
ERR_OUT_OF_RANGE,
},
hideStackFrames,
uvErrmapGet,
Expand Down Expand Up @@ -99,6 +98,7 @@ const {
validateOffsetLengthRead,
validateOffsetLengthWrite,
validatePath,
validatePosition,
validateRmOptions,
validateRmOptionsSync,
validateRmdirOptions,
Expand Down Expand Up @@ -556,19 +556,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 @@ -622,19 +610,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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ const {
validateAbortSignal,
validateBoolean,
validateInt32,
validateUint32
validateInteger,
validateUint32,
} = require('internal/validators');
const pathModule = require('path');
const kType = Symbol('type');
Expand Down Expand Up @@ -797,6 +798,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 @@ -820,6 +837,7 @@ module.exports = {
validateOffsetLengthRead,
validateOffsetLengthWrite,
validatePath,
validatePosition,
validateRmOptions,
validateRmOptionsSync,
validateRmdirOptions,
Expand Down

0 comments on commit dc8b210

Please sign in to comment.