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>
PR-URL: #42772
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
2 people authored and nodejs-github-bot committed Sep 11, 2022
1 parent 53633c0 commit 29953a0
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 @@ -77,6 +77,7 @@ const {
validateBuffer,
validateEncoding,
validateInteger,
validateObject,
validateString,
} = require('internal/validators');
const pathModule = require('path');
Expand Down Expand Up @@ -517,6 +518,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 29953a0

Please sign in to comment.