Skip to content

Commit 849319a

Browse files
connor4312brendanashworth
authored andcommitted
util: Check input to util.inherits
PR-URL: #1240 Reviewed-By: Brendan Ashworth <brendan.ashworth@me.com> Reviewed-By: Petka Antonov <petka_antonov@hotmail.com>
1 parent fe4434b commit 849319a

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/util.js

+15
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,23 @@ exports.log = function() {
616616
* @param {function} ctor Constructor function which needs to inherit the
617617
* prototype.
618618
* @param {function} superCtor Constructor function to inherit prototype from.
619+
* @throws {TypeError} Will error if either constructor is null, or if
620+
* the super constructor lacks a prototype.
619621
*/
620622
exports.inherits = function(ctor, superCtor) {
623+
624+
if (ctor === undefined || ctor === null)
625+
throw new TypeError('The constructor to `inherits` must not be ' +
626+
'null or undefined.');
627+
628+
if (superCtor === undefined || superCtor === null)
629+
throw new TypeError('The super constructor to `inherits` must not ' +
630+
'be null or undefined.');
631+
632+
if (superCtor.prototype === undefined)
633+
throw new TypeError('The super constructor to `inherits` must ' +
634+
'have a prototype.');
635+
621636
ctor.super_ = superCtor;
622637
ctor.prototype = Object.create(superCtor.prototype, {
623638
constructor: {

test/parallel/test-util.js

+7
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,10 @@ assert.deepEqual(util._extend({a:1}, true), {a:1});
7878
assert.deepEqual(util._extend({a:1}, false), {a:1});
7979
assert.deepEqual(util._extend({a:1}, {b:2}), {a:1, b:2});
8080
assert.deepEqual(util._extend({a:1, b:2}, {b:3}), {a:1, b:3});
81+
82+
// inherits
83+
var ctor = function() {};
84+
assert.throws(function() { util.inherits(ctor, {}) }, TypeError);
85+
assert.throws(function() { util.inherits(ctor, null) }, TypeError);
86+
assert.throws(function() { util.inherits(null, ctor) }, TypeError);
87+
assert.doesNotThrow(function() { util.inherits(ctor, ctor) }, TypeError);

0 commit comments

Comments
 (0)