Skip to content

Commit c21458a

Browse files
thefourtheyeaddaleax
authored andcommitted
buffer: expose underlying buffer object always
If the Buffer object's length is zero, or equal to the underlying buffer object's length, `parent` property returns `undefined`. > new Buffer(0).parent undefined > new Buffer(Buffer.poolSize).parent undefined This patch makes the buffer objects to consistently expose the buffer object via the `parent` property, always. Fixes: #8266 PR-URL: #8311 Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent e9b6fbb commit c21458a

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

lib/buffer.js

-4
Original file line numberDiff line numberDiff line change
@@ -410,10 +410,6 @@ Object.defineProperty(Buffer.prototype, 'parent', {
410410
get: function() {
411411
if (!(this instanceof Buffer))
412412
return undefined;
413-
if (this.byteLength === 0 ||
414-
this.byteLength === this.buffer.byteLength) {
415-
return undefined;
416-
}
417413
return this.buffer;
418414
}
419415
});

test/parallel/test-buffer-alloc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ if (common.hasCrypto) {
994994

995995
const ps = Buffer.poolSize;
996996
Buffer.poolSize = 0;
997-
assert.strictEqual(Buffer.allocUnsafe(1).parent, undefined);
997+
assert(Buffer.allocUnsafe(1).parent instanceof ArrayBuffer);
998998
Buffer.poolSize = ps;
999999

10001000
// Test Buffer.copy() segfault

test/parallel/test-buffer-arraybuffer.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ const buf = Buffer.from(ab);
1313

1414

1515
assert.ok(buf instanceof Buffer);
16-
// For backwards compatibility of old .parent property test that if buf is not
17-
// a slice then .parent should be undefined.
18-
assert.equal(buf.parent, undefined);
16+
assert.equal(buf.parent, buf.buffer);
1917
assert.equal(buf.buffer, ab);
2018
assert.equal(buf.length, ab.byteLength);
2119

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
/*
4+
* Fix for https://github.com/nodejs/node/issues/8266
5+
*
6+
* Zero length Buffer objects should expose the `buffer` property of the
7+
* TypedArrays, via the `parent` property.
8+
*/
9+
require('../common');
10+
const assert = require('assert');
11+
12+
// If the length of the buffer object is zero
13+
assert((new Buffer(0)).parent instanceof ArrayBuffer);
14+
15+
// If the length of the buffer object is equal to the underlying ArrayBuffer
16+
assert((new Buffer(Buffer.poolSize)).parent instanceof ArrayBuffer);
17+
18+
// Same as the previous test, but with user created buffer
19+
const arrayBuffer = new ArrayBuffer(0);
20+
assert.strictEqual(new Buffer(arrayBuffer).parent, arrayBuffer);
21+
assert.strictEqual(new Buffer(arrayBuffer).buffer, arrayBuffer);
22+
assert.strictEqual(Buffer.from(arrayBuffer).parent, arrayBuffer);
23+
assert.strictEqual(Buffer.from(arrayBuffer).buffer, arrayBuffer);

0 commit comments

Comments
 (0)