diff --git a/format.cc b/format.cc index 666f69c2..b70095b7 100644 --- a/format.cc +++ b/format.cc @@ -633,12 +633,15 @@ FMT_FUNC fmt::internal::UTF16ToUTF8::UTF16ToUTF8(fmt::WStringRef s) { } FMT_FUNC int fmt::internal::UTF16ToUTF8::convert(fmt::WStringRef s) { - int length = WideCharToMultiByte(CP_UTF8, 0, s.data(), s.size(), 0, 0, 0, 0); + if (s.size() > INT_MAX) + return ERROR_INVALID_PARAMETER; + int s_size = static_cast(s.size()); + int length = WideCharToMultiByte(CP_UTF8, 0, s.data(), s_size, 0, 0, 0, 0); if (length == 0) return GetLastError(); buffer_.resize(length + 1); length = WideCharToMultiByte( - CP_UTF8, 0, s.data(), s.size(), &buffer_[0], length, 0, 0); + CP_UTF8, 0, s.data(), s_size, &buffer_[0], length, 0, 0); if (length == 0) return GetLastError(); buffer_[length] = 0; diff --git a/test/util-test.cc b/test/util-test.cc index 2173b1bf..ea0e66f7 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -752,6 +752,7 @@ TEST(UtilTest, UTF8ToUTF16Error) { TEST(UtilTest, UTF16ToUTF8Convert) { fmt::internal::UTF16ToUTF8 u; EXPECT_EQ(ERROR_INVALID_PARAMETER, u.convert(fmt::WStringRef(0, 0))); + EXPECT_EQ(ERROR_INVALID_PARAMETER, u.convert(fmt::WStringRef("foo", INT_MAX + 1u))); } #endif // _WIN32