From e501ce4b218fd462d20b45ba817f6232219e6fa2 Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Sat, 5 Jan 2013 22:14:50 +0100 Subject: [PATCH] buffer: zero-length buffers shouldn't be slab-backed --- lib/buffer.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 9c57cad7a83..4958b31d3a8 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -199,8 +199,9 @@ function coerce(length) { } -// Buffer +var zeroBuffer = new SlowBuffer(0); +// Buffer function Buffer(subject, encoding, offset) { if (!(this instanceof Buffer)) { return new Buffer(subject, encoding, offset); @@ -242,13 +243,18 @@ function Buffer(subject, encoding, offset) { this.parent = new SlowBuffer(this.length); this.offset = 0; - } else { + } else if (this.length > 0) { // Small buffer. if (!pool || pool.length - pool.used < this.length) allocPool(); this.parent = pool; this.offset = pool.used; pool.used += this.length; if (pool.used & 7) pool.used = (pool.used + 8) & ~7; + + } else { + // Zero-length buffer + this.parent = zeroBuffer; + this.offset = 0; } // Treat array-ish objects as a byte array.