Skip empty strings in ToUtf(8|16).
We've observed a crash on Windows when the strings are empty, skipping the conversion seems reasonable in that case.
Bug: None
Change-Id: I3acf3060a88741fb750d7a0cc02e9422713c59cd
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/147380
Commit-Queue: Noah Richards <noahric@chromium.org>
Reviewed-by: Stefan Holmer <stefan@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#28709}
diff --git a/rtc_base/string_utils.h b/rtc_base/string_utils.h
index f8ad126..3518702 100644
--- a/rtc_base/string_utils.h
+++ b/rtc_base/string_utils.h
@@ -47,6 +47,8 @@
#if defined(WEBRTC_WIN)
inline std::wstring ToUtf16(const char* utf8, size_t len) {
+ if (len == 0)
+ return std::wstring();
int len16 = ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len),
nullptr, 0);
std::wstring ws(len16, 0);
@@ -60,6 +62,8 @@
}
inline std::string ToUtf8(const wchar_t* wide, size_t len) {
+ if (len == 0)
+ return std::string();
int len8 = ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len),
nullptr, 0, nullptr, nullptr);
std::string ns(len8, 0);