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: Adjust default length for fs.readSync and fsPromises/read #42128

Merged
merged 6 commits into from Feb 27, 2022
Merged
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
2 changes: 1 addition & 1 deletion doc/api/fs.md
Expand Up @@ -5396,7 +5396,7 @@ changes:
* `buffer` {Buffer|TypedArray|DataView}
* `options` {Object}
* `offset` {integer} **Default:** `0`
* `length` {integer} **Default:** `buffer.byteLength`
* `length` {integer} **Default:** `buffer.byteLength - offset`
* `position` {integer|bigint} **Default:** `null`
* Returns: {number}

Expand Down
14 changes: 9 additions & 5 deletions lib/fs.js
Expand Up @@ -604,7 +604,7 @@ function read(fd, buffer, offset, length, position, callback) {

if (arguments.length <= 3) {
// Assume fs.read(fd, options, callback)
let options = {};
let options = ObjectCreate(null);
if (arguments.length < 3) {
// This is fs.read(fd, callback)
// buffer will be the callback
Expand All @@ -621,7 +621,7 @@ function read(fd, buffer, offset, length, position, callback) {
buffer = Buffer.alloc(16384),
offset = 0,
length = buffer.byteLength - offset,
position
position = null
} = options);
}

Expand Down Expand Up @@ -686,10 +686,14 @@ function readSync(fd, buffer, offset, length, position) {
validateBuffer(buffer);

if (arguments.length <= 3) {
// Assume fs.read(fd, buffer, options)
const options = offset || {};
// Assume fs.readSync(fd, buffer, options)
const options = offset || ObjectCreate(null);

({ offset = 0, length = buffer.byteLength, position } = options);
({
offset = 0,
length = buffer.byteLength - offset,
position = null
} = options);
}

if (offset == null) {
Expand Down
22 changes: 10 additions & 12 deletions lib/internal/fs/promises.js
Expand Up @@ -5,6 +5,7 @@ const {
Error,
MathMax,
MathMin,
ObjectCreate,
NumberIsSafeInteger,
Promise,
PromisePrototypeThen,
Expand Down Expand Up @@ -510,18 +511,15 @@ async function open(path, flags, mode) {
async function read(handle, bufferOrOptions, offset, length, position) {
let buffer = bufferOrOptions;
if (!isArrayBufferView(buffer)) {
if (bufferOrOptions === undefined) {
bufferOrOptions = {};
}
if (bufferOrOptions.buffer) {
buffer = bufferOrOptions.buffer;
validateBuffer(buffer);
} else {
buffer = Buffer.alloc(16384);
}
offset = bufferOrOptions.offset || 0;
length = bufferOrOptions.length ?? buffer.byteLength;
position = bufferOrOptions.position ?? null;
bufferOrOptions ??= ObjectCreate(null);
({
buffer = Buffer.alloc(16384),
offset = 0,
length = buffer.byteLength - offset,
position = null
} = bufferOrOptions);
LiviaMedeiros marked this conversation as resolved.
Show resolved Hide resolved

validateBuffer(buffer);
}

if (offset == null) {
Expand Down
54 changes: 42 additions & 12 deletions test/parallel/test-fs-readSync-optional-params.js
Expand Up @@ -5,23 +5,53 @@ const fixtures = require('../common/fixtures');
const fs = require('fs');
const assert = require('assert');
const filepath = fixtures.path('x.txt');
const fd = fs.openSync(filepath, 'r');

const expected = Buffer.from('xyz\n');

function runTest(defaultBuffer, options) {
const result = fs.readSync(fd, defaultBuffer, options);
assert.strictEqual(result, expected.length);
assert.deepStrictEqual(defaultBuffer, expected);
let fd;
try {
fd = fs.openSync(filepath, 'r');
const result = fs.readSync(fd, defaultBuffer, options);
assert.strictEqual(result, expected.length);
assert.deepStrictEqual(defaultBuffer, expected);
} finally {
if (fd != null) fs.closeSync(fd);
}
}

// Test passing in an empty options object
runTest(Buffer.allocUnsafe(expected.length), { position: 0 });
for (const options of [

// Test not passing in any options object
runTest(Buffer.allocUnsafe(expected.length));
// Test options object
{ offset: 0 },
{ length: expected.length },
{ position: 0 },
{ offset: 0, length: expected.length },
{ offset: 0, position: 0 },
{ length: expected.length, position: 0 },
{ offset: 0, length: expected.length, position: 0 },

// Test passing in options
runTest(Buffer.allocUnsafe(expected.length), { offset: 0,
length: expected.length,
position: 0 });
{ offset: null },
{ position: null },
{ position: -1 },
{ position: 0n },

// Test default params
{},
null,
undefined,

// Test if bad params are interpreted as default (not mandatory)
false,
true,
Infinity,
42n,
Symbol(),
LiviaMedeiros marked this conversation as resolved.
Show resolved Hide resolved

// Test even more malicious corner cases
'4'.repeat(expected.length),
new String('4444'),
[4, 4, 4, 4],
]) {
runTest(Buffer.allocUnsafe(expected.length), options);
}