History

Address more bugs found during limbo compiler testing

arr committed 9 years ago

Commit 84ac3311 parent 220aaf26 files · +52 −63Browse files

Showing the changes to src/vm/string.cpp (1 of 6 files). Show all

Unified · Split

src/vm/string.cpp+16−30
@@ -301,23 +301,17 @@ vm_string_t::vm_string_t(const vm_string_t &other, word_t begin_index, word_t en · 300 unmodified lines
301301
302302 vm_string_t::~vm_string_t()
303303 {
304− // Free memory and string buffer
305− if (_is_alloc)
306− {
307− if (static_cast<void *>(_mem.alloc) != static_cast<void *>(_encoded_str))
308− free_memory(_encoded_str);
304+ // Determine if local or allocated memory
305+ auto source = (_is_alloc) ? _mem.alloc : _mem.local;
306+ if (static_cast<void *>(source) != static_cast<void *>(_encoded_str))
307+ free_memory(_encoded_str);
309308
310− free_memory(_mem.alloc);
311− }
312− else
313− {
314− if (static_cast<void *>(_mem.local) != static_cast<void *>(_encoded_str))
315− free_memory(_encoded_str);
316− }
309+ if (_is_alloc)
310+ free_memory(source);
317311
318312 debug::log_msg(debug::component_trace_t::memory, debug::log_level_t::debug, "destroy: vm string");
319313
320− debug::assign_debug_pointer(&_mem.alloc);
314+ debug::assign_debug_pointer(&source);
321315 debug::assign_debug_pointer(&_encoded_str);
322316 }
323317
@@ -341,14 +335,10 @@ vm_string_t &vm_string_t::append(const vm_string_t &other) · 17 unmodified lines
341335 const auto local_source = (_is_alloc) ? _mem.alloc : _mem.local;
342336
343337 // Release the encoded string for this instance.
344− {
345− std::lock_guard<std::mutex> lock{ _encoded_str_lock };
346− if (_encoded_str != reinterpret_cast<char*>(local_source))
347− {
348− free_memory(_encoded_str);
349− _encoded_str = nullptr;
350− }
351− }
338+ if (_encoded_str != reinterpret_cast<char*>(local_source))
339+ free_memory(_encoded_str);
340+
341+ _encoded_str = nullptr;
352342
353343 // Define the values for the appended string
354344 const auto new_length = local_length + other_length;
@@ -427,17 +417,12 @@ void vm_string_t::set_rune(word_t index, rune_t value) · 72 unmodified lines
427417 throw index_out_of_range_memory{ 0, _length, index };
428418
429419 // Determine if local or allocated memory
430− auto source = static_cast<uint8_t *>(_mem.local);
431− if (_is_alloc)
432− {
433− source = static_cast<uint8_t *>(_mem.alloc);
420+ auto source = (_is_alloc) ? _mem.alloc : _mem.local;
434421
435− // Since we are setting a rune, the string value should be freed.
436− if (_encoded_str != nullptr)
437− free_memory(_encoded_str);
438− }
422+ // Release the encoded string for this instance.
423+ if (_encoded_str != reinterpret_cast<char*>(source))
424+ free_memory(_encoded_str);
439425
440− // Reset the string so it can be regenerated.
441426 _encoded_str = nullptr;
442427
443428 const auto new_character_size = (value > utf8::constants::max_codepoint_ascii) ? sizeof(rune_t) : sizeof(char);
@@ -513,6 +498,7 @@ const char *vm_string_t::str() const · 69 unmodified lines
513498 if (_character_size == sizeof(char))
514499 {
515500 _encoded_str = (_is_alloc) ? reinterpret_cast<char *>(_mem.alloc) : reinterpret_cast<char *>(_mem.local);
501+ _encoded_str[_length] = '\0';
516502 return _encoded_str;
517503 }
518504