summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrevor Norris <trev.norris@gmail.com>2015-10-09 14:18:54 -0600
committerTrevor Norris <trev.norris@gmail.com>2015-10-09 15:42:23 -0600
commite97dae573c11ae6db072aa41d00e4422d38f68c7 (patch)
treefa5eee81b67a08dcb4b37974a230a61bda90a768
parent775c01e3aa0610112b1e7348dad8873cb8245fe5 (diff)
downloadnode-new-e97dae573c11ae6db072aa41d00e4422d38f68c7.tar.gz
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 <info@bnoordhuis.nl> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
-rw-r--r--lib/buffer.js4
-rw-r--r--test/parallel/test-buffer.js7
2 files changed, 11 insertions, 0 deletions
diff --git a/lib/buffer.js b/lib/buffer.js
index 98f6537931..41d5e0aba2 100644
--- a/lib/buffer.js
+++ b/lib/buffer.js
@@ -304,6 +304,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;
@@ -314,6 +316,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 ab9cea3078..1be4f3b842 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);