From 54795620f652271840b400b6f6d7581d128d5628 Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Fri, 9 Oct 2015 14:18:54 -0600 Subject: [PATCH] buffer: don't abort on prototype getters Accessing prototype properties directly on a typed array will throw. So do an extra check in Buffer's own getters to verify it is being called on an instance. Fixes: https://github.com/nodejs/node/issues/3297 PR-URL: https://github.com/nodejs/node/pull/3302 Reviewed-By: Ben Noordhuis Reviewed-By: Jeremiah Senkpiel --- lib/buffer.js | 4 ++++ test/parallel/test-buffer.js | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/lib/buffer.js b/lib/buffer.js index c2bcc769df1cf0..0fdfcecfdf9360 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -308,6 +308,8 @@ Buffer.byteLength = byteLength; Object.defineProperty(Buffer.prototype, 'parent', { enumerable: true, get: function() { + if (!(this instanceof Buffer)) + return undefined; if (this.byteLength === 0 || this.byteLength === this.buffer.byteLength) { return undefined; @@ -318,6 +320,8 @@ Object.defineProperty(Buffer.prototype, 'parent', { Object.defineProperty(Buffer.prototype, 'offset', { enumerable: true, get: function() { + if (!(this instanceof Buffer)) + return undefined; return this.byteOffset; } }); diff --git a/test/parallel/test-buffer.js b/test/parallel/test-buffer.js index ab9cea30782844..1be4f3b8424352 100644 --- a/test/parallel/test-buffer.js +++ b/test/parallel/test-buffer.js @@ -1224,3 +1224,10 @@ assert.throws(function() { assert.throws(function() { new Buffer(null); }, /must start with number, buffer, array or string/); + + +// Test prototype getters don't throw +assert.equal(Buffer.prototype.parent, undefined); +assert.equal(Buffer.prototype.offset, undefined); +assert.equal(SlowBuffer.prototype.parent, undefined); +assert.equal(SlowBuffer.prototype.offset, undefined);