From b702da4b8ca6e9842fcd49d2dc03f9bd437c0e8c Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Mon, 3 May 2021 17:18:23 +0800 Subject: [PATCH 1/2] fs: use `assert` in `fsCall` argument checking In the user perspective, it's not an arguemnt type error. Replace it with an `assert` expression. --- lib/internal/fs/promises.js | 6 +++--- test/parallel/test-fs-promises.js | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index e7f38219178952..6e0a3f175adca9 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -80,6 +80,7 @@ const { promisify } = require('internal/util'); const { EventEmitterMixin } = require('internal/event_target'); const { watch } = require('internal/fs/watchers'); const { isIterable } = require('internal/streams/utils'); +const assert = require('internal/assert'); const kHandle = Symbol('kHandle'); const kFd = Symbol('kFd'); @@ -266,9 +267,8 @@ async function handleFdClose(fileOpPromise, closeFunc) { } async function fsCall(fn, handle, ...args) { - if (handle[kRefs] === undefined) { - throw new ERR_INVALID_ARG_TYPE('filehandle', 'FileHandle', handle); - } + assert(handle instanceof FileHandle, + 'handle must be an instance of FileHandle'); if (handle.fd === -1) { // eslint-disable-next-line no-restricted-syntax diff --git a/test/parallel/test-fs-promises.js b/test/parallel/test-fs-promises.js index df7fa3e4733cba..f0140084732056 100644 --- a/test/parallel/test-fs-promises.js +++ b/test/parallel/test-fs-promises.js @@ -452,6 +452,16 @@ async function getHandle(dest) { assert.strictEqual(ret.bytesWritten, 2); await handle.close(); } + + // Test prototype methods calling with contexts other than FileHandle + { + const handle = await getHandle(dest); + assert.rejects(() => handle.stat.call({}), { + code: 'ERR_INTERNAL_ASSERTION', + message: /handle must be an instance of FileHandle/ + }); + await handle.close(); + } } doTest().then(common.mustCall()); From d2a6b490d7c863eb4ac07948df33851d86848e3e Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Thu, 6 May 2021 16:45:31 +0800 Subject: [PATCH 2/2] Update lib/internal/fs/promises.js Co-authored-by: Antoine du Hamel --- lib/internal/fs/promises.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 6e0a3f175adca9..7a575e69491e38 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -267,7 +267,7 @@ async function handleFdClose(fileOpPromise, closeFunc) { } async function fsCall(fn, handle, ...args) { - assert(handle instanceof FileHandle, + assert(handle[kRefs] !== undefined, 'handle must be an instance of FileHandle'); if (handle.fd === -1) {