Skip to content

Commit

Permalink
util: Check input to util.inherits
Browse files Browse the repository at this point in the history
PR-URL: #1240
Reviewed-By: Brendan Ashworth <[email protected]>
Reviewed-By: Petka Antonov <[email protected]>
  • Loading branch information
connor4312 authored and brendanashworth committed Mar 23, 2015
1 parent fe4434b commit 849319a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,23 @@ 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 (ctor === undefined || ctor === 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 or undefined.');

if (superCtor.prototype === undefined)
throw new TypeError('The super constructor to `inherits` must ' +
'have a prototype.');

ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 849319a

Please sign in to comment.