util: Check input to util.inherits
authorConnor Peet <connor@peet.io>
Mon, 23 Mar 2015 00:45:16 +0000 (20:45 -0400)
committerBrendan Ashworth <brendan.ashworth@me.com>
Mon, 23 Mar 2015 03:45:35 +0000 (20:45 -0700)
PR-URL: https://github.com/iojs/io.js/pull/1240
Reviewed-By: Brendan Ashworth <brendan.ashworth@me.com>
Reviewed-By: Petka Antonov <petka_antonov@hotmail.com>
lib/util.js
test/parallel/test-util.js

index fb5f7f4..69c46e6 100644 (file)
@@ -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: {
index 2fb4bc5..03456d6 100644 (file)
@@ -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);