Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: add validatePosition and use in read and readSync #37051

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -549,19 +549,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 @@ -615,19 +603,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 @@ -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
Expand All @@ -848,6 +865,7 @@ module.exports = {
validateOffsetLengthRead,
validateOffsetLengthWrite,
validatePath,
validatePosition,
validateRmOptions,
validateRmOptionsSync,
validateRmdirOptions,
Expand Down