fs: validate position argument before length === 0 early return · nodejs/node@63c111c · GitHub
Skip to content

Commit 63c111c

Browse files
geeksilva97aduh95
authored andcommitted
fs: validate position argument before length === 0 early return
PR-URL: #62674 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 6cd368b commit 63c111c

5 files changed

Lines changed: 84 additions & 19 deletions

File tree

‎lib/fs.js‎

Lines changed: 12 additions & 12 deletions

‎lib/internal/fs/promises.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,12 @@ async function read(handle, bufferOrParams, offset, length, position) {
678678

679679
length ??= buffer.byteLength - offset;
680680

681+
if (position == null) {
682+
position = -1;
683+
} else {
684+
validatePosition(position, 'position', length);
685+
}
686+
681687
if (length === 0)
682688
return { __proto__: null, bytesRead: length, buffer };
683689

@@ -688,12 +694,6 @@ async function read(handle, bufferOrParams, offset, length, position) {
688694

689695
validateOffsetLengthRead(offset, length, buffer.byteLength);
690696

691-
if (position == null) {
692-
position = -1;
693-
} else {
694-
validatePosition(position, 'position', length);
695-
}
696-
697697
const bytesRead = (await PromisePrototypeThen(
698698
binding.read(handle.fd, buffer, offset, length, position, kUsePromises),
699699
undefined,

‎test/parallel/test-fs-read-position-validation.mjs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,26 @@ async function testInvalid(code, position) {
9191
await testInvalid('ERR_INVALID_ARG_TYPE', badTypeValue);
9292
}
9393
}
94+
95+
{
96+
const emptyBuffer = Buffer.alloc(0);
97+
await new Promise((resolve, reject) => {
98+
fs.open(filepath, 'r', common.mustSucceed((fd) => {
99+
try {
100+
assert.throws(
101+
() => fs.read(fd, emptyBuffer, 0, 0, { not: 'a number' }, common.mustNotCall()),
102+
{ code: 'ERR_INVALID_ARG_TYPE' }
103+
);
104+
assert.throws(
105+
() => fs.read(fd, { buffer: emptyBuffer, offset: 0, length: 0, position: 'string' }, common.mustNotCall()),
106+
{ code: 'ERR_INVALID_ARG_TYPE' }
107+
);
108+
resolve();
109+
} catch (err) {
110+
reject(err);
111+
} finally {
112+
fs.close(fd, common.mustSucceed());
113+
}
114+
}));
115+
});
116+
}

‎test/parallel/test-fs-read-promises-position-validation.mjs‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,30 @@ async function testInvalid(code, position) {
7979
for (const badTypeValue of [
8080
false, true, '1', Symbol(1), {}, [], () => {}, Promise.resolve(1),
8181
]) {
82-
testInvalid('ERR_INVALID_ARG_TYPE', badTypeValue);
82+
await testInvalid('ERR_INVALID_ARG_TYPE', badTypeValue);
83+
}
84+
}
85+
86+
{
87+
const emptyBuffer = Buffer.alloc(0);
88+
let fh;
89+
try {
90+
fh = await fs.promises.open(filepath, 'r');
91+
await assert.rejects(
92+
fh.read(emptyBuffer, 0, 0, { not: 'a number' }),
93+
{ code: 'ERR_INVALID_ARG_TYPE' }
94+
);
95+
} finally {
96+
await fh?.close();
97+
}
98+
99+
try {
100+
fh = await fs.promises.open(filepath, 'r');
101+
await assert.rejects(
102+
fh.read({ buffer: emptyBuffer, offset: 0, length: 0, position: 'string' }),
103+
{ code: 'ERR_INVALID_ARG_TYPE' }
104+
);
105+
} finally {
106+
await fh?.close();
83107
}
84108
}

‎test/parallel/test-fs-readSync-position-validation.mjs‎

Lines changed: 18 additions & 0 deletions

0 commit comments

Comments
 (0)