Showing the changes to src/vm/string.cpp (1 of 6 files). Show all
Unified · Split
| @@ -191,10 +191,15 @@ vm_string_t::vm_string_t(std::size_t encoded_str_len, const uint8_t *encoded_str · 190 unmodified lines | |||
| 191 | , _mem{} | 191 | , _mem{} |
| 192 | { | 192 | { |
| 193 | assert((encoded_str_len > 0 && encoded_str != nullptr) || (encoded_str_len == 0 && encoded_str == nullptr)); | 193 | assert((encoded_str_len > 0 && encoded_str != nullptr) || (encoded_str_len == 0 && encoded_str == nullptr)); |
| 194 | auto character_count = utf8::count_codepoints(encoded_str, encoded_str_len); | 194 | const auto utf8_length = utf8::count_codepoints(encoded_str, encoded_str_len); |
| 195 | _character_size = (encoded_str_len != character_count) ? sizeof(rune_t) : sizeof(char); | ||
| 196 | 195 | ||
| 197 | _length = static_cast<word_t>(character_count); | 196 | // Check if the supplied string is completely valid UTF-8 - only valid UTF-8 strings can be DisVM strings. |
| 197 | if (encoded_str_len != utf8_length.byte_count) | ||
| 198 | throw invalid_utf8{}; | ||
| 199 | |||
| 200 | _character_size = (encoded_str_len != utf8_length.codepoint_count) ? sizeof(rune_t) : sizeof(char); | ||
| 201 | |||
| 202 | _length = static_cast<word_t>(utf8_length.codepoint_count); | ||
| 198 | _length_max = sizeof(_mem.local) / _character_size; | 203 | _length_max = sizeof(_mem.local) / _character_size; |
| 199 | 204 | ||
| 200 | // Allocate memory based on character count and size | 205 | // Allocate memory based on character count and size |