Skip to content

Commit

Permalink
buffer: don't abort on prototype getters
Browse files Browse the repository at this point in the history
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: #3297
PR-URL: #3302
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
  • Loading branch information
trevnorris authored and jasnell committed Oct 10, 2015
1 parent 071c72a commit 5479562
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
});
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 5479562

Please sign in to comment.