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: nodejs#3297
  • Loading branch information
trevnorris committed Oct 9, 2015
1 parent 775c01e commit 63aaab1
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 @@ -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;
Expand All @@ -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;
}
});
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 63aaab1

Please sign in to comment.