| Index: runtime/vm/dart_api_message.cc
|
| diff --git a/runtime/vm/dart_api_message.cc b/runtime/vm/dart_api_message.cc
|
| index 4cee392ee75564f19ef8bdd909d8116b9ed76e27..455ca2530fa699e288f1fd73ca3b350527b1c62e 100644
|
| --- a/runtime/vm/dart_api_message.cc
|
| +++ b/runtime/vm/dart_api_message.cc
|
| @@ -389,17 +389,27 @@ Dart_CObject* ApiMessageReader::ReadInternalVMObject(intptr_t class_id,
|
| uint16_t *utf16 =
|
| reinterpret_cast<uint16_t*>(::malloc(len * sizeof(uint16_t)));
|
| intptr_t utf8_len = 0;
|
| + // Read all the UTF-16 code units.
|
| for (intptr_t i = 0; i < len; i++) {
|
| utf16[i] = Read<uint16_t>();
|
| - // TODO(sgjesse): Check for surrogate pairs.
|
| - utf8_len += Utf8::Length(utf16[i]);
|
| + }
|
| + // Calculate the UTF-8 length and check if the string can be
|
| + // UTF-8 encoded.
|
| + bool valid = true;
|
| + Utf16::CodePointIterator it(utf16, len);
|
| + while (it.Next() && valid) {
|
| + utf8_len += Utf8::Length(it.Current());
|
| + valid = !Utf16::IsSurrogate(it.Current());
|
| + }
|
| + if (!valid) {
|
| + return AllocateDartCObjectUnsupported();
|
| }
|
| Dart_CObject* object = AllocateDartCObjectString(utf8_len);
|
| AddBackRef(object_id, object, kIsDeserialized);
|
| char* p = object->value.as_string;
|
| - for (intptr_t i = 0; i < len; i++) {
|
| - // TODO(sgjesse): Check for surrogate pairs.
|
| - p += Utf8::Encode(utf16[i], p);
|
| + it.Reset();
|
| + while (it.Next()) {
|
| + p += Utf8::Encode(it.Current(), p);
|
| }
|
| *p = '\0';
|
| ASSERT(p == (object->value.as_string + utf8_len));
|
| @@ -807,7 +817,11 @@ bool ApiMessageWriter::WriteCObjectInlined(Dart_CObject* object,
|
| if (type == Utf8::kLatin1) {
|
| uint8_t* latin1_str =
|
| reinterpret_cast<uint8_t*>(::malloc(len * sizeof(uint8_t)));
|
| - Utf8::DecodeToLatin1(utf8_str, utf8_len, latin1_str, len);
|
| + bool success = Utf8::DecodeToLatin1(utf8_str,
|
| + utf8_len,
|
| + latin1_str,
|
| + len);
|
| + ASSERT(success);
|
| for (intptr_t i = 0; i < len; i++) {
|
| Write<uint8_t>(latin1_str[i]);
|
| }
|
| @@ -816,7 +830,8 @@ bool ApiMessageWriter::WriteCObjectInlined(Dart_CObject* object,
|
| // TODO(sgjesse): Make sure surrogate pairs are handled.
|
| uint16_t* utf16_str =
|
| reinterpret_cast<uint16_t*>(::malloc(len * sizeof(uint16_t)));
|
| - Utf8::DecodeToUTF16(utf8_str, utf8_len, utf16_str, len);
|
| + bool success = Utf8::DecodeToUTF16(utf8_str, utf8_len, utf16_str, len);
|
| + ASSERT(success);
|
| for (intptr_t i = 0; i < len; i++) {
|
| Write<uint16_t>(utf16_str[i]);
|
| }
|
|
|