diff --git a/lib/net.js b/lib/net.js index 0e04cc45ad..58da9f735f 100644 --- a/lib/net.js +++ b/lib/net.js @@ -453,8 +453,6 @@ Stream.prototype._writeOut = function (data, encoding) { } else { // default to utf8 bytesWritten = pool.utf8Write(data, pool.used); - // Don't include the null - if (pool[pool.used + bytesWritten-1] == 0) bytesWritten--; // XXX Hacky way to find out the number of characters written. // Waiting for a more optimal way: http://codereview.chromium.org/1539013 var _s = pool.utf8Slice(pool.used, pool.used + bytesWritten); diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 813f5657ed..7c32d6ec60 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -310,6 +310,8 @@ Handle Buffer::Utf8Write(const Arguments &args) { int written = s->WriteUtf8((char*)p, buffer->length_ - offset); + if (written > 0 && p[written-1] == '\0') written--; + return scope.Close(Integer::New(written)); } diff --git a/test/simple/test-buffer.js b/test/simple/test-buffer.js index 18fe7e62e8..88282c5b0a 100644 --- a/test/simple/test-buffer.js +++ b/test/simple/test-buffer.js @@ -84,3 +84,14 @@ b[6] = 0xBE; b[7] = 0xEF; assert.deepEqual([0xDEADBEEF], b.unpack('N', 4)); + + +// Bug regression test +var testValue = '\u00F6\u65E5\u672C\u8A9E'; // ö日本語 +var buffer = new Buffer(32); +var size = buffer.utf8Write(testValue, 0); +puts('bytes written to buffer: ' + size); +var slice = buffer.utf8Slice(0, size); +assert.equal(slice, testValue); + +