Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer: expose underlying buffer object always #8311

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,6 @@ Object.defineProperty(Buffer.prototype, 'parent', {
get: function() {
if (!(this instanceof Buffer))
return undefined;
if (this.byteLength === 0 ||
this.byteLength === this.buffer.byteLength) {
return undefined;
}
return this.buffer;
}
});
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-buffer-alloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ if (common.hasCrypto) {

const ps = Buffer.poolSize;
Buffer.poolSize = 0;
assert.strictEqual(Buffer.allocUnsafe(1).parent, undefined);
assert(Buffer.allocUnsafe(1).parent instanceof ArrayBuffer);
Buffer.poolSize = ps;

// Test Buffer.copy() segfault
Expand Down
4 changes: 1 addition & 3 deletions test/parallel/test-buffer-arraybuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ const buf = Buffer.from(ab);


assert.ok(buf instanceof Buffer);
// For backwards compatibility of old .parent property test that if buf is not
// a slice then .parent should be undefined.
assert.equal(buf.parent, undefined);
assert.equal(buf.parent, buf.buffer);
assert.equal(buf.buffer, ab);
assert.equal(buf.length, ab.byteLength);

Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-buffer-parent-property.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

/*
* Fix for https://github.com/nodejs/node/issues/8266
*
* Zero length Buffer objects should expose the `buffer` property of the
* TypedArrays, via the `parent` property.
*/
require('../common');
const assert = require('assert');

// If the length of the buffer object is zero
assert((new Buffer(0)).parent instanceof ArrayBuffer);

// If the length of the buffer object is equal to the underlying ArrayBuffer
assert((new Buffer(Buffer.poolSize)).parent instanceof ArrayBuffer);

// Same as the previous test, but with user created buffer
const arrayBuffer = new ArrayBuffer(0);
assert.strictEqual(new Buffer(arrayBuffer).parent, arrayBuffer);
assert.strictEqual(new Buffer(arrayBuffer).buffer, arrayBuffer);
assert.strictEqual(Buffer.from(arrayBuffer).parent, arrayBuffer);
assert.strictEqual(Buffer.from(arrayBuffer).buffer, arrayBuffer);