Skip to content

Commit 6f54a14

Browse files
RaisinTentargos
authored andcommitted
fs: add validatePosition and use in read and readSync
PR-URL: #37051 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent db38cf2 commit 6f54a14

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

lib/fs.js

+3-27
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ const {
7575
ERR_INVALID_ARG_VALUE,
7676
ERR_INVALID_ARG_TYPE,
7777
ERR_FEATURE_UNAVAILABLE_ON_PLATFORM,
78-
ERR_OUT_OF_RANGE,
7978
},
8079
hideStackFrames,
8180
uvErrmapGet,
@@ -105,6 +104,7 @@ const {
105104
validateOffsetLengthRead,
106105
validateOffsetLengthWrite,
107106
validatePath,
107+
validatePosition,
108108
validateRmOptions,
109109
validateRmOptionsSync,
110110
validateRmdirOptions,
@@ -550,19 +550,7 @@ function read(fd, buffer, offset, length, position, callback) {
550550
if (position == null)
551551
position = -1;
552552

553-
if (typeof position === 'number') {
554-
validateInteger(position, 'position');
555-
} else if (typeof position === 'bigint') {
556-
if (!(position >= -(2n ** 63n) && position <= 2n ** 63n - 1n)) {
557-
throw new ERR_OUT_OF_RANGE('position',
558-
`>= ${-(2n ** 63n)} && <= ${2n ** 63n - 1n}`,
559-
position);
560-
}
561-
} else {
562-
throw new ERR_INVALID_ARG_TYPE('position',
563-
['integer', 'bigint'],
564-
position);
565-
}
553+
validatePosition(position, 'position');
566554

567555
function wrapper(err, bytesRead) {
568556
// 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) {
616604
if (position == null)
617605
position = -1;
618606

619-
if (typeof position === 'number') {
620-
validateInteger(position, 'position');
621-
} else if (typeof position === 'bigint') {
622-
if (!(position >= -(2n ** 63n) && position <= 2n ** 63n - 1n)) {
623-
throw new ERR_OUT_OF_RANGE('position',
624-
`>= ${-(2n ** 63n)} && <= ${2n ** 63n - 1n}`,
625-
position);
626-
}
627-
} else {
628-
throw new ERR_INVALID_ARG_TYPE('position',
629-
['integer', 'bigint'],
630-
position);
631-
}
607+
validatePosition(position, 'position');
632608

633609
const ctx = {};
634610
const result = binding.read(fd, buffer, offset, length, position,

lib/internal/fs/utils.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ const {
4747
validateAbortSignal,
4848
validateBoolean,
4949
validateInt32,
50-
validateUint32
50+
validateInteger,
51+
validateUint32,
5152
} = require('internal/validators');
5253
const pathModule = require('path');
5354
const kType = Symbol('type');
@@ -818,6 +819,22 @@ const validateStringAfterArrayBufferView = hideStackFrames((buffer, name) => {
818819
);
819820
});
820821

822+
const validatePosition = hideStackFrames((position, name) => {
823+
if (typeof position === 'number') {
824+
validateInteger(position, 'position');
825+
} else if (typeof position === 'bigint') {
826+
if (!(position >= -(2n ** 63n) && position <= 2n ** 63n - 1n)) {
827+
throw new ERR_OUT_OF_RANGE('position',
828+
`>= ${-(2n ** 63n)} && <= ${2n ** 63n - 1n}`,
829+
position);
830+
}
831+
} else {
832+
throw new ERR_INVALID_ARG_TYPE('position',
833+
['integer', 'bigint'],
834+
position);
835+
}
836+
});
837+
821838
module.exports = {
822839
assertEncoding,
823840
BigIntStats, // for testing
@@ -841,6 +858,7 @@ module.exports = {
841858
validateOffsetLengthRead,
842859
validateOffsetLengthWrite,
843860
validatePath,
861+
validatePosition,
844862
validateRmOptions,
845863
validateRmOptionsSync,
846864
validateRmdirOptions,

0 commit comments

Comments
 (0)