Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Fix Buffer drops last null character in UTF-8
Browse files Browse the repository at this point in the history
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
koichik committed Jul 10, 2011
1 parent 7e8735b commit 6079f1a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
}
Expand All @@ -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));
}
Expand Down
27 changes: 27 additions & 0 deletions test/simple/test-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 6079f1a

Please sign in to comment.