Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format long wstring (>500) will cause crash #173

Closed
BluewareJ opened this issue May 12, 2022 · 1 comment
Closed

format long wstring (>500) will cause crash #173

BluewareJ opened this issue May 12, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@BluewareJ
Copy link

I have corrected the code as below, please merge.

void wstring_to_utf8(fmt::wmemory_buffer const& w_mem_buffer, fmt::memory_buffer& mem_buffer)
{
auto remain_space = static_cast<int32_t>(mem_buffer.capacity() - mem_buffer.size());
auto bytes_needed = remain_space;

// TODO: so far utf-8 will use at most 4 bytes for a character.
if ((w_mem_buffer.size() + 1) * 4 > static_cast<size_t>(remain_space))
{
// if our given string is larger than the capacity, calculate how many bytes we need
bytes_needed = ::WideCharToMultiByte(
CP_UTF8, 0, w_mem_buffer.data(), static_cast(w_mem_buffer.size()), NULL, 0, NULL, NULL);

mem_buffer.reserve(static_cast<uint32_t>(bytes_needed + 1 + mem_buffer.size())); // +1 for EOL symbol, i.e. '\n'

}

if (QUILL_UNLIKELY(bytes_needed == 0))
{
auto const error = std::error_code(GetLastError(), std::system_category());
std::ostringstream error_msg;
error_msg << "wstring_to_utf8 failed with error message "
<< """ << error.message() << "", errno "" << error.value() << """;
QUILL_THROW(QuillError{error_msg.str()});
}

// convert
bytes_needed =
::WideCharToMultiByte(CP_UTF8, 0, w_mem_buffer.data(), static_cast(w_mem_buffer.size()),
mem_buffer.data() + mem_buffer.size(), bytes_needed, NULL, NULL);

if (QUILL_UNLIKELY(bytes_needed == 0))
{
auto const error = std::error_code(GetLastError(), std::system_category());
std::ostringstream error_msg;
error_msg << "wstring_to_utf8 failed with error message "
<< """ << error.message() << "", errno "" << error.value() << """;
QUILL_THROW(QuillError{error_msg.str()});
}

// set the new size
mem_buffer.resize(static_cast<uint32_t>(bytes_needed + mem_buffer.size()));
}

@odygrd odygrd closed this as completed in 4789a6a May 14, 2022
@odygrd odygrd added the bug Something isn't working label May 14, 2022
@odygrd
Copy link
Owner

odygrd commented May 14, 2022

Should be now fixed on master. Thanks a lot for reporting this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants