From eddea815e37fd69efc4dc52da0ede25363145fd2 Mon Sep 17 00:00:00 2001 From: Connor Peet Date: Sun, 22 Mar 2015 20:45:16 -0400 Subject: [PATCH 1/4] Check input to util.inherits --- lib/util.js | 10 ++++++++++ test/parallel/test-util.js | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/lib/util.js b/lib/util.js index fb5f7f4b31ab3d..e4dd64521b7c27 100644 --- a/lib/util.js +++ b/lib/util.js @@ -616,8 +616,18 @@ exports.log = function() { * @param {function} ctor Constructor function which needs to inherit the * prototype. * @param {function} superCtor Constructor function to inherit prototype from. + * @throws {TypeError} Will error if either constructor is null, or if + * the super constructor lacks a prototype. */ exports.inherits = function(ctor, superCtor) { + + if (isNullOrUndefined(ctor)) + throw new TypeError('The constructor to `inherits` must not be null.'); + if (isNullOrUndefined(superCtor)) + throw new TypeError('The super constructor to `inherits` must not be null.'); + if (isNullOrUndefined(superCtor.prototype)) + throw new TypeError('The super constructor must have a prototype.'); + ctor.super_ = superCtor; ctor.prototype = Object.create(superCtor.prototype, { constructor: { diff --git a/test/parallel/test-util.js b/test/parallel/test-util.js index 2fb4bc5609bcd6..03456d64393842 100644 --- a/test/parallel/test-util.js +++ b/test/parallel/test-util.js @@ -78,3 +78,10 @@ assert.deepEqual(util._extend({a:1}, true), {a:1}); assert.deepEqual(util._extend({a:1}, false), {a:1}); assert.deepEqual(util._extend({a:1}, {b:2}), {a:1, b:2}); assert.deepEqual(util._extend({a:1, b:2}, {b:3}), {a:1, b:3}); + +// inherits +var ctor = function() {}; +assert.throws(function() { util.inherits(ctor, {}) }, TypeError); +assert.throws(function() { util.inherits(ctor, null) }, TypeError); +assert.throws(function() { util.inherits(null, ctor) }, TypeError); +assert.doesNotThrow(function() { util.inherits(ctor, ctor) }, TypeError); From e92728884ddddc85cf973092dd038afe3c693ec0 Mon Sep 17 00:00:00 2001 From: Connor Peet Date: Sun, 22 Mar 2015 20:49:23 -0400 Subject: [PATCH 2/4] Prototype is allowed to be null, just not undefined --- lib/util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/util.js b/lib/util.js index e4dd64521b7c27..116e5e89d72c7a 100644 --- a/lib/util.js +++ b/lib/util.js @@ -625,7 +625,7 @@ exports.inherits = function(ctor, superCtor) { throw new TypeError('The constructor to `inherits` must not be null.'); if (isNullOrUndefined(superCtor)) throw new TypeError('The super constructor to `inherits` must not be null.'); - if (isNullOrUndefined(superCtor.prototype)) + if (isUndefined(superCtor.prototype)) throw new TypeError('The super constructor must have a prototype.'); ctor.super_ = superCtor; From 7131380bde82c61a8008895fa1932427b02fef8c Mon Sep 17 00:00:00 2001 From: Connor Peet Date: Sun, 22 Mar 2015 22:54:33 -0400 Subject: [PATCH 3/4] Use manual checks --- lib/util.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/util.js b/lib/util.js index 116e5e89d72c7a..42de70419472b5 100644 --- a/lib/util.js +++ b/lib/util.js @@ -621,11 +621,11 @@ exports.log = function() { */ exports.inherits = function(ctor, superCtor) { - if (isNullOrUndefined(ctor)) + if (ctor === undefined || ctor === null) throw new TypeError('The constructor to `inherits` must not be null.'); - if (isNullOrUndefined(superCtor)) + if (superCtor === undefined || superCtor === null) throw new TypeError('The super constructor to `inherits` must not be null.'); - if (isUndefined(superCtor.prototype)) + if (superCtor.prototype === undefined) throw new TypeError('The super constructor must have a prototype.'); ctor.super_ = superCtor; From f32b4027298a34e106900dd1342ef5983376b2d4 Mon Sep 17 00:00:00 2001 From: Connor Peet Date: Sun, 22 Mar 2015 23:03:21 -0400 Subject: [PATCH 4/4] Line length --- lib/util.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/util.js b/lib/util.js index 42de70419472b5..69c46e6ea7a9fb 100644 --- a/lib/util.js +++ b/lib/util.js @@ -622,11 +622,16 @@ exports.log = function() { exports.inherits = function(ctor, superCtor) { if (ctor === undefined || ctor === null) - throw new TypeError('The constructor to `inherits` must not be null.'); + throw new TypeError('The constructor to `inherits` must not be ' + + 'null or undefined.'); + if (superCtor === undefined || superCtor === null) - throw new TypeError('The super constructor to `inherits` must not be null.'); + throw new TypeError('The super constructor to `inherits` must not ' + + 'be null or undefined.'); + if (superCtor.prototype === undefined) - throw new TypeError('The super constructor must have a prototype.'); + throw new TypeError('The super constructor to `inherits` must ' + + 'have a prototype.'); ctor.super_ = superCtor; ctor.prototype = Object.create(superCtor.prototype, {