Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Don't let v8::String::Utf8Write() append '\0' to the output buffer.

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...
commit 22fabd428c9498ccf9c18afd3976fc0e5b40cc1f 1 parent 13a521e
Ben Noordhuis authored

Showing 1 changed file with 6 additions and 5 deletions. Show diff stats Hide diff stats

  1. +6 5 src/node_buffer.cc
11 src/node_buffer.cc
@@ -475,9 +475,12 @@ Handle<Value> Buffer::Utf8Write(const Arguments &args) {
475 475 "Offset is out of bounds")));
476 476 }
477 477
478   - size_t max_length = args[2]->IsUndefined() ? buffer->length_ - offset
479   - : args[2]->Uint32Value();
480   - max_length = MIN(buffer->length_ - offset, max_length);
  478 + const size_t utf8len = s->Utf8Length();
  479 + const size_t len =
  480 + args[2]->IsUint32()
  481 + ? MIN(utf8len, args[2]->Uint32Value())
  482 + : utf8len;
  483 + const size_t max_length = MIN(len, buffer->length_ - offset);
481 484
482 485 char* p = buffer->data_ + offset;
483 486
@@ -491,8 +494,6 @@ Handle<Value> Buffer::Utf8Write(const Arguments &args) {
491 494 constructor_template->GetFunction()->Set(chars_written_sym,
492 495 Integer::New(char_written));
493 496
494   - if (written > 0 && p[written-1] == '\0') written--;
495   -
496 497 return scope.Close(Integer::New(written));
497 498 }
498 499

0 comments on commit 22fabd4

Please sign in to comment.
Something went wrong with that request. Please try again.