Showing the changes to src/vm/string.cpp (1 of 6 files). Show all
Unified · Split
| @@ -123,14 +123,14 @@ namespace · 122 unmodified lines | ||
| 123 | 123 | template<> |
| 124 | 124 | void copy_characters_to(char *dest, word_t dest_start, const char *source, word_t source_length) |
| 125 | 125 | { |
| 126 | − assert(dest != nullptr && source != nullptr); | |
| 126 | + assert(dest != nullptr && source != nullptr && dest != source); | |
| 127 | 127 | std::memcpy((dest + dest_start), source, source_length); |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | template<> |
| 131 | 131 | void copy_characters_to(runtime::rune_t *dest, word_t dest_start, const runtime::rune_t *source, word_t source_length) |
| 132 | 132 | { |
| 133 | − assert(dest != nullptr && source != nullptr); | |
| 133 | + assert(dest != nullptr && source != nullptr && dest != source); | |
| 134 | 134 | std::memcpy((dest + dest_start), source, (source_length * sizeof(runtime::rune_t))); |
| 135 | 135 | } |
| 136 | 136 | |
| @@ -286,17 +286,19 @@ vm_string_t::vm_string_t(const vm_string_t &other, word_t begin_index, word_t en · 149 unmodified lines | ||
| 286 | 286 | // Allocate memory based on character count and size |
| 287 | 287 | auto src = other._mem.local; |
| 288 | 288 | auto dest = _mem.local; |
| 289 | − const auto length_in_bytes = _length * _character_size; | |
| 290 | 289 | if (_is_alloc) |
| 291 | 290 | { |
| 292 | 291 | src = other._mem.alloc; |
| 293 | − _mem.alloc = alloc_memory<uint8_t>(_length_max); | |
| 292 | + _mem.alloc = alloc_memory<uint8_t>(_length_max * _character_size); | |
| 294 | 293 | dest = _mem.alloc; |
| 295 | 294 | } |
| 296 | 295 | |
| 297 | 296 | // Copy over string content starting at the correct index (char vs. rune_t) |
| 297 | + const auto length_in_bytes = _length * _character_size; | |
| 298 | 298 | std::memcpy(dest, (src + (begin_index * _character_size)), length_in_bytes); |
| 299 | − debug::log_msg(debug::component_trace_t::memory, debug::log_level_t::debug, "copy: vm string: %#" PRIxPTR " %d %d %#" PRIxPTR, &other, begin_index, end_index, this); | |
| 299 | + | |
| 300 | + if (debug::is_component_tracing_enabled<debug::component_trace_t::memory>()) | |
| 301 | + debug::log_msg(debug::component_trace_t::memory, debug::log_level_t::debug, "copy: vm string: %#" PRIxPTR " %d %d %#" PRIxPTR, &other, begin_index, end_index, this); | |
| 300 | 302 | } |
| 301 | 303 | |
| 302 | 304 | vm_string_t::~vm_string_t() |
| @@ -309,10 +311,11 @@ vm_string_t::~vm_string_t() · 6 unmodified lines | ||
| 309 | 311 | if (_is_alloc) |
| 310 | 312 | free_memory(source); |
| 311 | 313 | |
| 312 | − debug::log_msg(debug::component_trace_t::memory, debug::log_level_t::debug, "destroy: vm string"); | |
| 313 | − | |
| 314 | 314 | debug::assign_debug_pointer(&source); |
| 315 | 315 | debug::assign_debug_pointer(&_encoded_str); |
| 316 | + | |
| 317 | + if (debug::is_component_tracing_enabled<debug::component_trace_t::memory>()) | |
| 318 | + debug::log_msg(debug::component_trace_t::memory, debug::log_level_t::debug, "destroy: vm string"); | |
| 316 | 319 | } |
| 317 | 320 | |
| 318 | 321 | vm_string_t &vm_string_t::append(const vm_string_t &other) |
| @@ -324,6 +327,7 @@ vm_string_t &vm_string_t::append(const vm_string_t &other) · 5 unmodified lines | ||
| 324 | 327 | |
| 325 | 328 | // Check if the new string has any length at all. |
| 326 | 329 | const auto other_length = other._length; |
| 330 | + assert(other_length >= 0); | |
| 327 | 331 | if (other_length == 0) |
| 328 | 332 | return *this; |
| 329 | 333 | |
| @@ -342,7 +346,7 @@ vm_string_t &vm_string_t::append(const vm_string_t &other) · 12 unmodified lines | ||
| 342 | 346 | |
| 343 | 347 | // Define the values for the appended string |
| 344 | 348 | const auto new_length = local_length + other_length; |
| 345 | − const auto new_length_max = (new_length == 0) ? static_cast<word_t>(compute_max_length(_mem.local)) : compute_max_length(new_length); | |
| 349 | + const auto new_length_max = compute_max_length(new_length); | |
| 346 | 350 | const auto new_is_rune = local_is_rune || other_is_rune; |
| 347 | 351 | uint8_t *new_source = nullptr; |
| 348 | 352 | |
| @@ -497,8 +501,15 @@ const char *vm_string_t::str() const · 148 unmodified lines | ||
| 497 | 501 | // If the string is ASCII then return the memory |
| 498 | 502 | if (_character_size == sizeof(char)) |
| 499 | 503 | { |
| 500 | − _encoded_str = (_is_alloc) ? reinterpret_cast<char *>(_mem.alloc) : reinterpret_cast<char *>(_mem.local); | |
| 501 | − _encoded_str[_length] = '\0'; | |
| 504 | + auto str = (_is_alloc) ? _mem.alloc : _mem.local; | |
| 505 | + if (_length_max <= _length) | |
| 506 | + { | |
| 507 | + auto tmp = alloc_memory<char>(_length + 1); // +1 for null | |
| 508 | + str = static_cast<uint8_t *>(std::memcpy(tmp, str, _length)); | |
| 509 | + } | |
| 510 | + | |
| 511 | + _encoded_str = reinterpret_cast<char *>(str); | |
| 512 | + assert(_encoded_str[_length] == '\0'); | |
| 502 | 513 | return _encoded_str; |
| 503 | 514 | } |
| 504 | 515 | |