Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(798)

Side by Side Diff: vm/dart_api_impl.cc

Issue 11275107: Fix length computation in Dart_StringToUTF8. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« bin/builtin_natives.cc ('K') | « include/dart_api.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "include/dart_api.h" 5 #include "include/dart_api.h"
6 6
7 #include "vm/bigint_operations.h" 7 #include "vm/bigint_operations.h"
8 #include "vm/class_finalizer.h" 8 #include "vm/class_finalizer.h"
9 #include "vm/compiler.h" 9 #include "vm/compiler.h"
10 #include "vm/dart.h" 10 #include "vm/dart.h"
(...skipping 1625 matching lines...) Expand 10 before | Expand all | Expand 10 after
1636 CHECK_LENGTH(length, String::kMaxElements); 1636 CHECK_LENGTH(length, String::kMaxElements);
1637 return Api::NewHandle(isolate, 1637 return Api::NewHandle(isolate,
1638 String::NewExternal(utf16_array, length, peer, cback)); 1638 String::NewExternal(utf16_array, length, peer, cback));
1639 } 1639 }
1640 1640
1641 1641
1642 DART_EXPORT Dart_Handle Dart_StringToCString(Dart_Handle object, 1642 DART_EXPORT Dart_Handle Dart_StringToCString(Dart_Handle object,
1643 const char** cstr) { 1643 const char** cstr) {
1644 Isolate* isolate = Isolate::Current(); 1644 Isolate* isolate = Isolate::Current();
1645 DARTSCOPE(isolate); 1645 DARTSCOPE(isolate);
1646 if (cstr == NULL) {
1647 RETURN_NULL_ERROR(cstr);
1648 }
1646 const String& str_obj = Api::UnwrapStringHandle(isolate, object); 1649 const String& str_obj = Api::UnwrapStringHandle(isolate, object);
1647 if (str_obj.IsNull()) { 1650 if (str_obj.IsNull()) {
1648 RETURN_TYPE_ERROR(isolate, object, String); 1651 RETURN_TYPE_ERROR(isolate, object, String);
1649 } 1652 }
1650 intptr_t string_length = Utf8::Length(str_obj); 1653 intptr_t string_length = Utf8::Length(str_obj);
1651 char* res = Api::TopScope(isolate)->zone()->Alloc<char>(string_length + 1); 1654 char* res = Api::TopScope(isolate)->zone()->Alloc<char>(string_length + 1);
1652 if (res == NULL) { 1655 if (res == NULL) {
1653 return Api::NewError("Unable to allocate memory"); 1656 return Api::NewError("Unable to allocate memory");
1654 } 1657 }
1655 const char* string_value = str_obj.ToCString(); 1658 const char* string_value = str_obj.ToCString();
1656 memmove(res, string_value, string_length + 1); 1659 memmove(res, string_value, string_length + 1);
1657 ASSERT(res[string_length] == '\0'); 1660 ASSERT(res[string_length] == '\0');
1658 *cstr = res; 1661 *cstr = res;
1659 return Api::Success(isolate); 1662 return Api::Success(isolate);
1660 } 1663 }
1661 1664
1662 1665
1663 DART_EXPORT Dart_Handle Dart_StringToUTF8(Dart_Handle str, 1666 DART_EXPORT Dart_Handle Dart_StringToUTF8(Dart_Handle str,
1664 uint8_t* utf8_array, 1667 uint8_t** utf8_array,
1665 intptr_t* length) { 1668 intptr_t* length) {
1666 Isolate* isolate = Isolate::Current(); 1669 Isolate* isolate = Isolate::Current();
1667 DARTSCOPE(isolate); 1670 DARTSCOPE(isolate);
1671 if (utf8_array == NULL) {
1672 RETURN_NULL_ERROR(utf8_array);
1673 }
1674 if (length == NULL) {
1675 RETURN_NULL_ERROR(length);
1676 }
1668 const String& str_obj = Api::UnwrapStringHandle(isolate, str); 1677 const String& str_obj = Api::UnwrapStringHandle(isolate, str);
1669 if (str_obj.IsNull()) { 1678 if (str_obj.IsNull()) {
1670 RETURN_TYPE_ERROR(isolate, str, String); 1679 RETURN_TYPE_ERROR(isolate, str, String);
1671 } 1680 }
1672 intptr_t str_len = str_obj.Length(); 1681 intptr_t str_len = Utf8::Length(str_obj);
1673 if (str_len > *length) { 1682 *utf8_array = Api::TopScope(isolate)->zone()->Alloc<uint8_t>(str_len);
1674 return Api::NewError("Input array is not large enough to hold the result"); 1683 if (*utf8_array == NULL) {
1684 return Api::NewError("Unable to allocate memory");
1675 } 1685 }
1676 str_obj.ToUTF8(utf8_array, str_len); 1686 str_obj.ToUTF8(*utf8_array, str_len);
1677 *length= str_len; 1687 *length = str_len;
1678 return Api::Success(isolate); 1688 return Api::Success(isolate);
1679 } 1689 }
1680 1690
1681 1691
1682 DART_EXPORT Dart_Handle Dart_StringToUTF16(Dart_Handle str, 1692 DART_EXPORT Dart_Handle Dart_StringToUTF16(Dart_Handle str,
1683 uint16_t* utf16_array, 1693 uint16_t* utf16_array,
1684 intptr_t* length) { 1694 intptr_t* length) {
1685 Isolate* isolate = Isolate::Current(); 1695 Isolate* isolate = Isolate::Current();
1686 DARTSCOPE(isolate); 1696 DARTSCOPE(isolate);
1687 const String& str_obj = Api::UnwrapStringHandle(isolate, str); 1697 const String& str_obj = Api::UnwrapStringHandle(isolate, str);
(...skipping 2793 matching lines...) Expand 10 before | Expand all | Expand 10 after
4481 } 4491 }
4482 { 4492 {
4483 NoGCScope no_gc; 4493 NoGCScope no_gc;
4484 RawObject* raw_obj = obj.raw(); 4494 RawObject* raw_obj = obj.raw();
4485 isolate->heap()->SetPeer(raw_obj, peer); 4495 isolate->heap()->SetPeer(raw_obj, peer);
4486 } 4496 }
4487 return Api::Success(isolate); 4497 return Api::Success(isolate);
4488 } 4498 }
4489 4499
4490 } // namespace dart 4500 } // namespace dart
OLDNEW
« bin/builtin_natives.cc ('K') | « include/dart_api.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698