From e11949a86ad178c92a76fa7fb45f8bf671ddb539 Mon Sep 17 00:00:00 2001 From: ERSUCC Date: Thu, 31 Oct 2024 15:19:14 -0500 Subject: [PATCH 1/3] Replaced deprecated string conversion function --- RtAudio.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/RtAudio.cpp b/RtAudio.cpp index ac6bee92..8539e7eb 100644 --- a/RtAudio.cpp +++ b/RtAudio.cpp @@ -45,10 +45,10 @@ #include #include #include +#include #include #include #include -#include #include #if defined(_WIN32) @@ -88,7 +88,20 @@ std::string convertCharPointerToStdString(const wchar_t* text) WideCharToMultiByte(CP_UTF8, 0, text, wchars, &nret[0], nchars, 0, 0); return nret; #else - return std::wstring_convert>{}.to_bytes(text); + std::string result; + char dest[MB_CUR_MAX]; + // get number of wide characters in text + size_t length = wcslen(text); + for (size_t i = 0; i < length; i++) { + // get number of converted bytes + int bytes = wctomb(dest, text[i]); + // protect against buffer overflow from conversion errors, + // or if the buffer is full and therefore not null-terminated + for (int i = 0; i < bytes; i++) { + result += dest[i]; + } + } + return result; #endif } From daa63b5db792f995a88094711cb2792b7c3b94d7 Mon Sep 17 00:00:00 2001 From: ERSUCC Date: Thu, 31 Oct 2024 15:28:55 -0500 Subject: [PATCH 2/3] Fixed double use of i in for loop --- RtAudio.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/RtAudio.cpp b/RtAudio.cpp index 8539e7eb..0724f083 100644 --- a/RtAudio.cpp +++ b/RtAudio.cpp @@ -97,8 +97,8 @@ std::string convertCharPointerToStdString(const wchar_t* text) int bytes = wctomb(dest, text[i]); // protect against buffer overflow from conversion errors, // or if the buffer is full and therefore not null-terminated - for (int i = 0; i < bytes; i++) { - result += dest[i]; + for (int j = 0; j < bytes; j++) { + result += dest[j]; } } return result; From 57a45cfb0818ab1d6a2747c7c0087e6009cc82e8 Mon Sep 17 00:00:00 2001 From: ERSUCC Date: Thu, 31 Oct 2024 15:33:06 -0500 Subject: [PATCH 3/3] Improved code security --- RtAudio.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/RtAudio.cpp b/RtAudio.cpp index 0724f083..ef484b4e 100644 --- a/RtAudio.cpp +++ b/RtAudio.cpp @@ -74,9 +74,9 @@ std::string convertCharPointerToStdString(const char *text) template<> inline std::string convertCharPointerToStdString(const wchar_t* text) { -#if defined(_MSC_VER) if (!text) return std::string(); +#if defined(_MSC_VER) const int wchars = (int)wcslen(text); // how many characters are required after conversion? const int nchars = WideCharToMultiByte(CP_UTF8, 0, text, wchars, 0, 0, 0, 0); @@ -91,10 +91,10 @@ std::string convertCharPointerToStdString(const wchar_t* text) std::string result; char dest[MB_CUR_MAX]; // get number of wide characters in text - size_t length = wcslen(text); + const size_t length = wcslen(text); for (size_t i = 0; i < length; i++) { // get number of converted bytes - int bytes = wctomb(dest, text[i]); + const int bytes = wctomb(dest, text[i]); // protect against buffer overflow from conversion errors, // or if the buffer is full and therefore not null-terminated for (int j = 0; j < bytes; j++) {