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

Side by Side Diff: runtime/vm/symbols.cc

Issue 1326853003: - Do not access the first character in zero-length strings. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 3 months 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
« no previous file with comments | « runtime/vm/coverage.cc ('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 "vm/symbols.h" 5 #include "vm/symbols.h"
6 6
7 #include "vm/handles.h" 7 #include "vm/handles.h"
8 #include "vm/handles_impl.h" 8 #include "vm/handles_impl.h"
9 #include "vm/hash_table.h" 9 #include "vm/hash_table.h"
10 #include "vm/isolate.h" 10 #include "vm/isolate.h"
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 } 418 }
419 const bool is_one_byte_string = char_size == kOneByteChar; 419 const bool is_one_byte_string = char_size == kOneByteChar;
420 420
421 Zone* zone = Thread::Current()->zone(); 421 Zone* zone = Thread::Current()->zone();
422 if (is_one_byte_string) { 422 if (is_one_byte_string) {
423 uint8_t* buffer = zone->Alloc<uint8_t>(len_sum); 423 uint8_t* buffer = zone->Alloc<uint8_t>(len_sum);
424 const uint8_t* const orig_buffer = buffer; 424 const uint8_t* const orig_buffer = buffer;
425 for (intptr_t i = 0; i < strs_length; i++) { 425 for (intptr_t i = 0; i < strs_length; i++) {
426 NoSafepointScope no_safepoint; 426 NoSafepointScope no_safepoint;
427 intptr_t str_len = lengths[i]; 427 intptr_t str_len = lengths[i];
428 const String& str = strs[i]; 428 if (str_len > 0) {
429 ASSERT(str.IsOneByteString() || str.IsExternalOneByteString()); 429 const String& str = strs[i];
430 const uint8_t* src_p = str.IsOneByteString() ? 430 ASSERT(str.IsOneByteString() || str.IsExternalOneByteString());
431 OneByteString::CharAddr(str, 0) : 431 const uint8_t* src_p = str.IsOneByteString() ?
432 ExternalOneByteString::CharAddr(str, 0); 432 OneByteString::CharAddr(str, 0) :
433 memmove(buffer, src_p, str_len); 433 ExternalOneByteString::CharAddr(str, 0);
434 buffer += str_len; 434 memmove(buffer, src_p, str_len);
435 buffer += str_len;
436 }
435 } 437 }
436 ASSERT(len_sum == buffer - orig_buffer); 438 ASSERT(len_sum == buffer - orig_buffer);
437 return Symbols::FromLatin1(orig_buffer, len_sum); 439 return Symbols::FromLatin1(orig_buffer, len_sum);
438 } else { 440 } else {
439 uint16_t* buffer = zone->Alloc<uint16_t>(len_sum); 441 uint16_t* buffer = zone->Alloc<uint16_t>(len_sum);
440 const uint16_t* const orig_buffer = buffer; 442 const uint16_t* const orig_buffer = buffer;
441 for (intptr_t i = 0; i < strs_length; i++) { 443 for (intptr_t i = 0; i < strs_length; i++) {
442 NoSafepointScope no_safepoint; 444 NoSafepointScope no_safepoint;
443 intptr_t str_len = lengths[i]; 445 intptr_t str_len = lengths[i];
444 const String& str = strs[i]; 446 if (str_len > 0) {
445 if (str.IsTwoByteString()) { 447 const String& str = strs[i];
446 memmove(buffer, TwoByteString::CharAddr(str, 0), str_len * 2); 448 if (str.IsTwoByteString()) {
447 } else if (str.IsExternalTwoByteString()) { 449 memmove(buffer, TwoByteString::CharAddr(str, 0), str_len * 2);
448 memmove(buffer, ExternalTwoByteString::CharAddr(str, 0), str_len * 2); 450 } else if (str.IsExternalTwoByteString()) {
449 } else { 451 memmove(buffer, ExternalTwoByteString::CharAddr(str, 0), str_len * 2);
450 // One-byte to two-byte string copy. 452 } else {
451 ASSERT(str.IsOneByteString() || str.IsExternalOneByteString()); 453 // One-byte to two-byte string copy.
452 const uint8_t* src_p = str.IsOneByteString() ? 454 ASSERT(str.IsOneByteString() || str.IsExternalOneByteString());
453 OneByteString::CharAddr(str, 0) : 455 const uint8_t* src_p = str.IsOneByteString() ?
454 ExternalOneByteString::CharAddr(str, 0); 456 OneByteString::CharAddr(str, 0) :
455 for (int n = 0; n < str_len; n++) { 457 ExternalOneByteString::CharAddr(str, 0);
456 buffer[n] = src_p[n]; 458 for (int n = 0; n < str_len; n++) {
459 buffer[n] = src_p[n];
460 }
457 } 461 }
462 buffer += str_len;
458 } 463 }
459 buffer += str_len;
460 } 464 }
461 ASSERT(len_sum == buffer - orig_buffer); 465 ASSERT(len_sum == buffer - orig_buffer);
462 return Symbols::FromUTF16(orig_buffer, len_sum); 466 return Symbols::FromUTF16(orig_buffer, len_sum);
463 } 467 }
464 } 468 }
465 469
466 470
467 // StringType can be StringSlice, ConcatString, or {Latin1,UTF16,UTF32}Array. 471 // StringType can be StringSlice, ConcatString, or {Latin1,UTF16,UTF32}Array.
468 template<typename StringType> 472 template<typename StringType>
469 RawString* Symbols::NewSymbol(const StringType& str) { 473 RawString* Symbols::NewSymbol(const StringType& str) {
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
540 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { 544 RawObject* Symbols::GetVMSymbol(intptr_t object_id) {
541 ASSERT(IsVMSymbolId(object_id)); 545 ASSERT(IsVMSymbolId(object_id));
542 intptr_t i = (object_id - kMaxPredefinedObjectIds); 546 intptr_t i = (object_id - kMaxPredefinedObjectIds);
543 if ((i > kIllegal) && (i < Symbols::kMaxPredefinedId)) { 547 if ((i > kIllegal) && (i < Symbols::kMaxPredefinedId)) {
544 return symbol_handles_[i]->raw(); 548 return symbol_handles_[i]->raw();
545 } 549 }
546 return Object::null(); 550 return Object::null();
547 } 551 }
548 552
549 } // namespace dart 553 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/coverage.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698