Skip to content

Commit 8bcade5

Browse files
committed
locale: update ctype access for MSVC CRT 14+
Visual C++ 14 and newer split msvcrt into msvcrt and ucrt with flavours of the ucrt for different environments. This changed the access to the ctype table by introducing the `__pctype_func` and `__pwctype_func` accessors. Use this rather than directly accessing `_ctype` which allows us to be safer in threaded situations by going through the libc locking. llvm-svn: 290823
1 parent 8412616 commit 8bcade5

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

libcxx/include/support/win32/locale_win32.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
#ifndef _LIBCPP_SUPPORT_WIN32_LOCALE_WIN32_H
1212
#define _LIBCPP_SUPPORT_WIN32_LOCALE_WIN32_H
1313

14+
#include <crtversion.h>
15+
16+
#if _VC_CRT_MAJOR_VERSION < 14
1417
// ctype mask table defined in msvcrt.dll
15-
extern "C" unsigned short __declspec(dllimport) _ctype[];
18+
extern "C" unsigned short __declspec(dllimport) _ctype[];
19+
#endif
1620

1721
#include "support/win32/support.h"
1822
#include "support/win32/locale_mgmt_win32.h"

libcxx/src/locale.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#endif
2525
#include "clocale"
2626
#include "cstring"
27+
#if defined(_LIBCPP_MSVCRT)
28+
#define _CTYPE_DISABLE_MACROS
29+
#endif
2730
#include "cwctype"
2831
#include "__sso_allocator"
2932
#if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
@@ -1108,9 +1111,13 @@ ctype<char>::classic_table() _NOEXCEPT
11081111
#elif __sun__
11091112
return __ctype_mask;
11101113
#elif defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
1114+
#if _VC_CRT_MAJOR_VERSION < 14
1115+
// This is assumed to be safe, which is a nonsense assumption because we're
1116+
// going to end up dereferencing it later...
11111117
return _ctype+1; // internal ctype mask table defined in msvcrt.dll
1112-
// This is assumed to be safe, which is a nonsense assumption because we're
1113-
// going to end up dereferencing it later...
1118+
#else
1119+
return __pctype_func();
1120+
#endif
11141121
#elif defined(__EMSCRIPTEN__)
11151122
return *__ctype_b_loc();
11161123
#elif defined(_NEWLIB_VERSION)

0 commit comments

Comments
 (0)