Browse Source

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.
v0.10.8-release
Timothy J Fontaine 12 years ago
parent
commit
007e63bb13
  1. 10
      src/node_buffer.cc
  2. 7
      test/simple/test-buffer.js

10
src/node_buffer.cc

@ -345,7 +345,15 @@ Handle<Value> Buffer::StringWrite(const Arguments& args) {
Local<String> str = args[0].As<String>();
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();

7
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);
});

Loading…
Cancel
Save