Skip to content

Commit 5479562

Browse files
trevnorrisjasnell
authored andcommitted
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: #3297 PR-URL: #3302 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent 071c72a commit 5479562

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

lib/buffer.js

+4
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ Buffer.byteLength = byteLength;
308308
Object.defineProperty(Buffer.prototype, 'parent', {
309309
enumerable: true,
310310
get: function() {
311+
if (!(this instanceof Buffer))
312+
return undefined;
311313
if (this.byteLength === 0 ||
312314
this.byteLength === this.buffer.byteLength) {
313315
return undefined;
@@ -318,6 +320,8 @@ Object.defineProperty(Buffer.prototype, 'parent', {
318320
Object.defineProperty(Buffer.prototype, 'offset', {
319321
enumerable: true,
320322
get: function() {
323+
if (!(this instanceof Buffer))
324+
return undefined;
321325
return this.byteOffset;
322326
}
323327
});

test/parallel/test-buffer.js

+7
Original file line numberDiff line numberDiff line change
@@ -1224,3 +1224,10 @@ assert.throws(function() {
12241224
assert.throws(function() {
12251225
new Buffer(null);
12261226
}, /must start with number, buffer, array or string/);
1227+
1228+
1229+
// Test prototype getters don't throw
1230+
assert.equal(Buffer.prototype.parent, undefined);
1231+
assert.equal(Buffer.prototype.offset, undefined);
1232+
assert.equal(SlowBuffer.prototype.parent, undefined);
1233+
assert.equal(SlowBuffer.prototype.offset, undefined);

0 commit comments

Comments
 (0)