@@ -465,21 +465,26 @@ inline std::wstring Utf8ToWString(const std::string& str) {
465465 const char * old_locale = setlocale (LC_CTYPE, nullptr );
466466 setlocale (LC_CTYPE, " en_US.UTF-8" );
467467
468+ // Get the required buffer size (excluding null terminator)
468469 size_t size_needed = mbstowcs (nullptr , str.c_str (), 0 );
469470 if (size_needed == static_cast <size_t >(-1 )) {
470471 LOG_ERROR (" mbstowcs failed for UTF8 to wide string conversion" );
471472 setlocale (LC_CTYPE, old_locale);
472473 return {};
473474 }
474475
475- std::wstring result (size_needed, 0 );
476- size_t converted = mbstowcs (&result[0 ], str.c_str (), size_needed);
476+ // Allocate buffer with space for null terminator
477+ std::wstring result (size_needed + 1 , 0 );
478+ // Convert with proper buffer size to prevent overflow
479+ size_t converted = mbstowcs (&result[0 ], str.c_str (), result.size ());
477480 if (converted == static_cast <size_t >(-1 )) {
478481 LOG_ERROR (" mbstowcs failed for UTF8 to wide string conversion" );
479482 setlocale (LC_CTYPE, old_locale);
480483 return {};
481484 }
482485
486+ // Resize to actual content length (excluding null terminator)
487+ result.resize (converted);
483488 setlocale (LC_CTYPE, old_locale);
484489 return result;
485490#endif
0 commit comments