From dda5e5f8a5d680328ca1bb287b82b5410e45e575 Mon Sep 17 00:00:00 2001 From: Voltrex <62040526+VoltrexMaster@users.noreply.github.com> Date: Tue, 1 Jun 2021 21:09:38 +0430 Subject: [PATCH 1/4] util: use missing validators The `inherits()` method in the `util` lib module is not using validators which others do use. --- lib/util.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/lib/util.js b/lib/util.js index a8d9a356480a35..b9e73a466bc368 100644 --- a/lib/util.js +++ b/lib/util.js @@ -63,6 +63,7 @@ const { debuglog } = require('internal/util/debuglog'); const { validateFunction, validateNumber, + validateObject, } = require('internal/validators'); const { TextDecoder, TextEncoder } = require('internal/encoding'); const { isBuffer } = require('buffer').Buffer; @@ -229,17 +230,10 @@ function log(...args) { * the super constructor lacks a prototype. */ function inherits(ctor, superCtor) { + validateFunction(ctor, 'ctor'); + validateFunction(superCtor, 'superCtor'); + validateObject(superCtor.prototype, 'superCtor.prototype'); - if (ctor === undefined || ctor === null) - throw new ERR_INVALID_ARG_TYPE('ctor', 'Function', ctor); - - if (superCtor === undefined || superCtor === null) - throw new ERR_INVALID_ARG_TYPE('superCtor', 'Function', superCtor); - - if (superCtor.prototype === undefined) { - throw new ERR_INVALID_ARG_TYPE('superCtor.prototype', - 'Object', superCtor.prototype); - } ObjectDefineProperty(ctor, 'super_', { value: superCtor, writable: true, From 5ed551978b5b6892755d85b19949a542d89eaf8b Mon Sep 17 00:00:00 2001 From: Voltrex <62040526+VoltrexMaster@users.noreply.github.com> Date: Tue, 1 Jun 2021 21:16:47 +0430 Subject: [PATCH 2/4] fixup! remove unused error constructor Removed the `ERR_INVALID_ARG_TYPE` constructor which was only used in the `inherits()` method which is replaced now. --- lib/util.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/util.js b/lib/util.js index b9e73a466bc368..7922846fee0426 100644 --- a/lib/util.js +++ b/lib/util.js @@ -47,7 +47,6 @@ const { const { codes: { ERR_FALSY_VALUE_REJECTION, - ERR_INVALID_ARG_TYPE, ERR_OUT_OF_RANGE }, errnoException, From 43c44914c681b0a7c482e2cd2ad7e94096dd33e4 Mon Sep 17 00:00:00 2001 From: Voltrex <62040526+VoltrexMaster@users.noreply.github.com> Date: Tue, 1 Jun 2021 22:54:48 +0430 Subject: [PATCH 3/4] fixup! pass valid test value The value must be a function to test the type of the prototype since validators first check if the value is a function. --- test/parallel/test-util-inherits.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-util-inherits.js b/test/parallel/test-util-inherits.js index 1729b1734d03db..6ce2b7da98ce71 100644 --- a/test/parallel/test-util-inherits.js +++ b/test/parallel/test-util-inherits.js @@ -82,9 +82,15 @@ assert.strictEqual(e.d(), 'd'); assert.strictEqual(e.e(), 'e'); assert.strictEqual(e.constructor, E); +function F() { + this.test = 'test'; +} + +F.prototype = null; + // Should throw with invalid arguments assert.throws(() => { - inherits(A, {}); + inherits(A, F); }, { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError', From 80b1d810f51cf8643f403d98c03e9d7b0147c4a2 Mon Sep 17 00:00:00 2001 From: Voltrex <62040526+VoltrexMaster@users.noreply.github.com> Date: Wed, 2 Jun 2021 00:34:53 +0430 Subject: [PATCH 4/4] fixup! prototype test value The message expects to fail with the value `undefined`, not `null`. --- test/parallel/test-util-inherits.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-util-inherits.js b/test/parallel/test-util-inherits.js index 6ce2b7da98ce71..ac835852fcb326 100644 --- a/test/parallel/test-util-inherits.js +++ b/test/parallel/test-util-inherits.js @@ -86,7 +86,7 @@ function F() { this.test = 'test'; } -F.prototype = null; +F.prototype = undefined; // Should throw with invalid arguments assert.throws(() => {