From 007e63bb13b5d3af54081972ab88217571828906 Mon Sep 17 00:00:00 2001 From: Timothy J Fontaine Date: Thu, 23 May 2013 16:23:07 -0700 Subject: [PATCH] buffer: special case empty string writes Prior to 119354f we specifically handled passing a zero length string to write on a buffer, restore that functionality. --- src/node_buffer.cc | 10 +++++++++- test/simple/test-buffer.js | 7 +++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 2dfddc7..8153c83 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -345,7 +345,15 @@ Handle Buffer::StringWrite(const Arguments& args) { Local str = args[0].As(); - if (encoding == HEX && str->Length() % 2 != 0) + int length = str->Length(); + + if (length == 0) { + constructor_template->GetFunction()->Set(chars_written_sym, + Integer::New(0)); + return scope.Close(Integer::New(0)); + } + + if (encoding == HEX && length % 2 != 0) return ThrowTypeError("Invalid hex string"); size_t offset = args[1]->Int32Value(); diff --git a/test/simple/test-buffer.js b/test/simple/test-buffer.js index 0d07112..3026824 100644 --- a/test/simple/test-buffer.js +++ b/test/simple/test-buffer.js @@ -998,3 +998,10 @@ assert.equal(Buffer.byteLength('aaaa==', 'base64'), 3); assert.throws(function() { Buffer('', 'buffer'); }, TypeError); + +assert.doesNotThrow(function () { + var slow = new SlowBuffer(1); + assert(slow.write('', Buffer.poolSize * 10) === 0); + var fast = new Buffer(1); + assert(fast.write('', Buffer.poolSize * 10) === 0); +}); -- 2.7.4