Skip to content

Make a deep copy of the returned value from setlocale #7458

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions include/dxc/WinAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ unsigned int SysStringLen(const BSTR bstrString);
// RAII style mechanism for setting/unsetting a locale for the specified Windows
// codepage
class ScopedLocale {
const char *m_prevLocale;
std::string m_prevLocale;

public:
explicit ScopedLocale(uint32_t codePage)
Expand All @@ -925,25 +925,21 @@ class ScopedLocale {
"Support for Linux only handles UTF8 code pages");
setlocale(LC_ALL, "en_US.UTF-8");
}
~ScopedLocale() {
if (m_prevLocale != nullptr) {
setlocale(LC_ALL, m_prevLocale);
}
}
~ScopedLocale() { setlocale(LC_ALL, m_prevLocale.c_str()); }
};

// The t_nBufferLength parameter is part of the published interface, but not
// used here.
template <int t_nBufferLength = 128> class CW2AEX {
public:
CW2AEX(LPCWSTR psz) {
ScopedLocale locale(CP_UTF8);

if (!psz) {
m_psz = NULL;
return;
}

ScopedLocale locale(CP_UTF8);

int len = (wcslen(psz) + 1) * 4;
m_psz = new char[len];
std::wcstombs(m_psz, psz, len);
Expand All @@ -962,13 +958,13 @@ typedef CW2AEX<> CW2A;
template <int t_nBufferLength = 128> class CA2WEX {
public:
CA2WEX(LPCSTR psz) {
ScopedLocale locale(CP_UTF8);

if (!psz) {
m_psz = NULL;
return;
}

ScopedLocale locale(CP_UTF8);

int len = strlen(psz) + 1;
m_psz = new wchar_t[len];
std::mbstowcs(m_psz, psz, len);
Expand Down
12 changes: 2 additions & 10 deletions lib/DxcSupport/Unicode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ int MultiByteToWideChar(uint32_t /*CodePage*/, uint32_t /*dwFlags*/,
}

size_t rv;
const char *prevLocale = setlocale(LC_ALL, nullptr);
setlocale(LC_ALL, "en_US.UTF-8");
ScopedLocale locale(CP_UTF8);
if (lpMultiByteStr[cbMultiByte - 1] != '\0') {
char *srcStr = (char *)malloc((cbMultiByte + 1) * sizeof(char));
strncpy(srcStr, lpMultiByteStr, cbMultiByte);
Expand All @@ -64,9 +63,6 @@ int MultiByteToWideChar(uint32_t /*CodePage*/, uint32_t /*dwFlags*/,
rv = mbstowcs(lpWideCharStr, lpMultiByteStr, cchWideChar);
}

if (prevLocale)
setlocale(LC_ALL, prevLocale);

if (rv == (size_t)cbMultiByte)
return rv;
return rv + 1; // mbstowcs excludes the terminating character
Expand Down Expand Up @@ -108,8 +104,7 @@ int WideCharToMultiByte(uint32_t /*CodePage*/, uint32_t /*dwFlags*/,
}

size_t rv;
const char *prevLocale = setlocale(LC_ALL, nullptr);
setlocale(LC_ALL, "en_US.UTF-8");
ScopedLocale locale(CP_UTF8);
if (lpWideCharStr[cchWideChar - 1] != L'\0') {
wchar_t *srcStr = (wchar_t *)malloc((cchWideChar + 1) * sizeof(wchar_t));
wcsncpy(srcStr, lpWideCharStr, cchWideChar);
Expand All @@ -120,9 +115,6 @@ int WideCharToMultiByte(uint32_t /*CodePage*/, uint32_t /*dwFlags*/,
rv = wcstombs(lpMultiByteStr, lpWideCharStr, cbMultiByte);
}

if (prevLocale)
setlocale(LC_ALL, prevLocale);

if (rv == (size_t)cchWideChar)
return rv;
return rv + 1; // mbstowcs excludes the terminating character
Expand Down