Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix Buffer drops last null character in UTF-8

Reproduce:

    $ node
    > buf = new Buffer('\0')
    <Buffer >
    > buf.length
    0
    > buf = new Buffer(1)
    <Buffer 28>
    > buf.write('\0')
    0

Fixes #394.
Fixes #1210.
  • Loading branch information...
commit 6079f1a3319ba0d61cc01a943be6c495a1433f85 1 parent 7e8735b
@koichik authored
Showing with 43 additions and 2 deletions.
  1. +16 −2 src/node_buffer.cc
  2. +27 −0 test/simple/test-buffer.js
View
18 src/node_buffer.cc
@@ -447,7 +447,15 @@ Handle<Value> Buffer::Utf8Write(const Arguments &args) {
size_t offset = args[1]->Uint32Value();
- if (s->Length() > 0 && offset >= buffer->length_) {
+ int length = s->Length();
+
+ if (length == 0) {
+ constructor_template->GetFunction()->Set(chars_written_sym,
+ Integer::New(0));
+ return scope.Close(Integer::New(0));
+ }
+
+ if (length > 0 && offset >= buffer->length_) {
return ThrowException(Exception::TypeError(String::New(
"Offset is out of bounds")));
}
@@ -468,7 +476,13 @@ Handle<Value> Buffer::Utf8Write(const Arguments &args) {
constructor_template->GetFunction()->Set(chars_written_sym,
Integer::New(char_written));
- if (written > 0 && p[written-1] == '\0') written--;
+ if (written > 0 && p[written-1] == '\0' && char_written == length) {
+ uint16_t last_char;
+ s->Write(&last_char, length - 1, 1, String::NO_HINTS);
+ if (last_char != 0 || written > s->Utf8Length()) {
+ written--;
+ }
+ }
return scope.Close(Integer::New(written));
}
View
27 test/simple/test-buffer.js
@@ -526,3 +526,30 @@ assert.equal(0xef, b[3]);
assert.throws(function() {
new Buffer('"pong"', 0, 6, 8031, '127.0.0.1')
});
+
+// #1210 Test UTF-8 string includes null character
+var buf = new Buffer('\0');
+assert.equal(buf.length, 1);
+buf = new Buffer('\0\0');
+assert.equal(buf.length, 2);
+
+buf = new Buffer(2);
+var written = buf.write(''); // 0byte
+assert.equal(written, 0);
+written = buf.write('\0'); // 1byte (v8 adds null terminator)
+assert.equal(written, 1);
+written = buf.write('a\0'); // 1byte * 2
+assert.equal(written, 2);
+written = buf.write(''); // 3bytes
+assert.equal(written, 0);
+written = buf.write('\0'); // 1byte + 3bytes
+assert.equal(written, 1);
+written = buf.write('\0\0'); // 1byte * 2 + 3bytes
+assert.equal(written, 2);
+
+buf = new Buffer(10);
+written = buf.write('あいう'); // 3bytes * 3 (v8 adds null terminator)
+assert.equal(written, 9);
+written = buf.write('あいう\0'); // 3bytes * 3 + 1byte
+assert.equal(written, 10);
+
Please sign in to comment.
Something went wrong with that request. Please try again.