Skip to content

Commit 402f772

Browse files
RaisinTenMylesBorins
authored andcommitted
fs: add validatePosition and use in read and readSync
PR-URL: #37051 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent f3be3ec commit 402f772

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
@@ -69,7 +69,6 @@ const {
6969
ERR_INVALID_ARG_TYPE,
7070
ERR_INVALID_CALLBACK,
7171
ERR_FEATURE_UNAVAILABLE_ON_PLATFORM,
72-
ERR_OUT_OF_RANGE,
7372
},
7473
hideStackFrames,
7574
uvErrmapGet,
@@ -99,6 +98,7 @@ const {
9998
validateOffsetLengthRead,
10099
validateOffsetLengthWrite,
101100
validatePath,
101+
validatePosition,
102102
validateRmOptions,
103103
validateRmOptionsSync,
104104
validateRmdirOptions,
@@ -556,19 +556,7 @@ function read(fd, buffer, offset, length, position, callback) {
556556
if (position == null)
557557
position = -1;
558558

559-
if (typeof position === 'number') {
560-
validateInteger(position, 'position');
561-
} else if (typeof position === 'bigint') {
562-
if (!(position >= -(2n ** 63n) && position <= 2n ** 63n - 1n)) {
563-
throw new ERR_OUT_OF_RANGE('position',
564-
`>= ${-(2n ** 63n)} && <= ${2n ** 63n - 1n}`,
565-
position);
566-
}
567-
} else {
568-
throw new ERR_INVALID_ARG_TYPE('position',
569-
['integer', 'bigint'],
570-
position);
571-
}
559+
validatePosition(position, 'position');
572560

573561
function wrapper(err, bytesRead) {
574562
// Retain a reference to buffer so that it can't be GC'ed too soon.
@@ -622,19 +610,7 @@ function readSync(fd, buffer, offset, length, position) {
622610
if (position == null)
623611
position = -1;
624612

625-
if (typeof position === 'number') {
626-
validateInteger(position, 'position');
627-
} else if (typeof position === 'bigint') {
628-
if (!(position >= -(2n ** 63n) && position <= 2n ** 63n - 1n)) {
629-
throw new ERR_OUT_OF_RANGE('position',
630-
`>= ${-(2n ** 63n)} && <= ${2n ** 63n - 1n}`,
631-
position);
632-
}
633-
} else {
634-
throw new ERR_INVALID_ARG_TYPE('position',
635-
['integer', 'bigint'],
636-
position);
637-
}
613+
validatePosition(position, 'position');
638614

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

lib/internal/fs/utils.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ const {
4242
validateAbortSignal,
4343
validateBoolean,
4444
validateInt32,
45-
validateUint32
45+
validateInteger,
46+
validateUint32,
4647
} = require('internal/validators');
4748
const pathModule = require('path');
4849
const kType = Symbol('type');
@@ -797,6 +798,22 @@ const validateStringAfterArrayBufferView = hideStackFrames((buffer, name) => {
797798
);
798799
});
799800

801+
const validatePosition = hideStackFrames((position, name) => {
802+
if (typeof position === 'number') {
803+
validateInteger(position, 'position');
804+
} else if (typeof position === 'bigint') {
805+
if (!(position >= -(2n ** 63n) && position <= 2n ** 63n - 1n)) {
806+
throw new ERR_OUT_OF_RANGE('position',
807+
`>= ${-(2n ** 63n)} && <= ${2n ** 63n - 1n}`,
808+
position);
809+
}
810+
} else {
811+
throw new ERR_INVALID_ARG_TYPE('position',
812+
['integer', 'bigint'],
813+
position);
814+
}
815+
});
816+
800817
module.exports = {
801818
assertEncoding,
802819
BigIntStats, // for testing
@@ -820,6 +837,7 @@ module.exports = {
820837
validateOffsetLengthRead,
821838
validateOffsetLengthWrite,
822839
validatePath,
840+
validatePosition,
823841
validateRmOptions,
824842
validateRmOptionsSync,
825843
validateRmdirOptions,

0 commit comments

Comments
 (0)