Skip to content

Commit

Permalink
fs: harden filehandle.read(params) typecheck
Browse files Browse the repository at this point in the history
Make sure that first argument is a nullable object

Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
LiviaMedeiros and aduh95 committed May 18, 2022
1 parent fe027da commit 761c078
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const {
validateBuffer,
validateEncoding,
validateInteger,
validateObject,
validateString,
} = require('internal/validators');
const pathModule = require('path');
Expand Down Expand Up @@ -512,6 +513,9 @@ async function read(handle, bufferOrParams, offset, length, position) {
let buffer = bufferOrParams;
if (!isArrayBufferView(buffer)) {
// This is fh.read(params)
if (bufferOrParams !== undefined) {
validateObject(bufferOrParams, 'options', { nullable: true });
}
({
buffer = Buffer.alloc(16384),
offset = 0,
Expand Down
14 changes: 12 additions & 2 deletions test/parallel/test-fs-promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,24 @@ async function executeOnHandle(dest, func) {
});
}

// Use fallback buffer allocation when input not buffer
// Use fallback buffer allocation when first argument is null
{
await executeOnHandle(dest, async (handle) => {
const ret = await handle.read(0, 0, 0, 0);
const ret = await handle.read(null, 0, 0, 0);
assert.strictEqual(ret.buffer.length, 16384);
});
}

// TypeError if buffer is not ArrayBufferView or nullable object
{
await executeOnHandle(dest, async (handle) => {
await assert.rejects(
async () => handle.read(0, 0, 0, 0),
{ code: 'ERR_INVALID_ARG_TYPE' }
);
});
}

// Bytes written to file match buffer
{
await executeOnHandle(dest, async (handle) => {
Expand Down

0 comments on commit 761c078

Please sign in to comment.