Workaround -Wstringop-overflow

This commit is contained in:
Victor Zverovich 2024-08-24 07:46:44 -07:00
parent c59ee969f3
commit 0379bf3a5d

View File

@ -665,6 +665,7 @@ FMT_CONSTEXPR void for_each_codepoint(string_view s, F f) {
string_view(ptr, error ? 1 : to_unsigned(end - buf_ptr))); string_view(ptr, error ? 1 : to_unsigned(end - buf_ptr)));
return result ? (error ? buf_ptr + 1 : end) : nullptr; return result ? (error ? buf_ptr + 1 : end) : nullptr;
}; };
auto p = s.data(); auto p = s.data();
const size_t block_size = 4; // utf8_decode always reads blocks of 4 chars. const size_t block_size = 4; // utf8_decode always reads blocks of 4 chars.
if (s.size() >= block_size) { if (s.size() >= block_size) {
@ -673,7 +674,10 @@ FMT_CONSTEXPR void for_each_codepoint(string_view s, F f) {
if (!p) return; if (!p) return;
} }
} }
if (auto num_chars_left = s.data() + s.size() - p) { auto num_chars_left = to_unsigned(s.data() + s.size() - p);
if (num_chars_left == 0) return;
FMT_ASSERT(num_chars_left < block_size, "");
char buf[2 * block_size - 1] = {}; char buf[2 * block_size - 1] = {};
copy<char>(p, p + num_chars_left, buf); copy<char>(p, p + num_chars_left, buf);
const char* buf_ptr = buf; const char* buf_ptr = buf;
@ -682,8 +686,7 @@ FMT_CONSTEXPR void for_each_codepoint(string_view s, F f) {
if (!end) return; if (!end) return;
p += end - buf_ptr; p += end - buf_ptr;
buf_ptr = end; buf_ptr = end;
} while (buf_ptr - buf < num_chars_left); } while (buf_ptr < buf + num_chars_left);
}
} }
template <typename Char> template <typename Char>