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

Commit

Permalink
Don't let v8::String::Utf8Write() append '\0' to the output buffer.
Browse files Browse the repository at this point in the history
v8::String::Utf8Write() appends '\0' if there is room left in
the output buffer. Pass in the exact decoded length obtained
with v8::String::Utf8Length() to make it stop doing that.

Fixes #394.
  • Loading branch information
bnoordhuis committed Jul 10, 2011
1 parent 13a521e commit 22fabd4
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,12 @@ Handle<Value> Buffer::Utf8Write(const Arguments &args) {
"Offset is out of bounds")));
}

size_t max_length = args[2]->IsUndefined() ? buffer->length_ - offset
: args[2]->Uint32Value();
max_length = MIN(buffer->length_ - offset, max_length);
const size_t utf8len = s->Utf8Length();
const size_t len =
args[2]->IsUint32()
? MIN(utf8len, args[2]->Uint32Value())
: utf8len;
const size_t max_length = MIN(len, buffer->length_ - offset);

char* p = buffer->data_ + offset;

Expand All @@ -491,8 +494,6 @@ 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--;

return scope.Close(Integer::New(written));
}

Expand Down

0 comments on commit 22fabd4

Please sign in to comment.