Skip to content

Commit b91d3e2

Browse files
[SYCL] protect against possible data corruption cause by std::locale side effect (#18578)
std::locale::global() in a user app can impact the SYCL library by side-effect, causing numbers (include hex) to be formatted with commas and periods where we might not expect such. In most cases, our use of std streams is for user facing messages. But in some cases they are used in places where unexpected formatting could cause errors. This PR attempts to shore these places up. Signed-off-by: Chris Perkins <[email protected]>
1 parent ba5cdff commit b91d3e2

File tree

4 files changed

+7
-0
lines changed

4 files changed

+7
-0
lines changed

sycl/include/syclcompat/kernel.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ static inline fs::path write_data_to_file(char const *const data, size_t size) {
112112

113113
// create private directory
114114
std::stringstream directory;
115+
directory.imbue(std::locale::classic()); // avoid locale issues, like commas
115116
fs::path directory_path;
116117
constexpr int max_attempts = 5;
117118
int i;
@@ -134,6 +135,7 @@ static inline fs::path write_data_to_file(char const *const data, size_t size) {
134135

135136
// random filename in private directory
136137
std::stringstream filename;
138+
filename.imbue(std::locale::classic());
137139
filename << std::hex << rand(prng);
138140
#ifdef _WIN32
139141
auto filepath = directory_path / (filename.str() + ".dll");

sycl/source/detail/kernel_compiler/kernel_compiler_opencl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ void SetupLibrary(voidPtr &oclocInvokeHandle, voidPtr &oclocFreeOutputHandle,
144144

145145
std::string IPVersionsToString(const std::vector<uint32_t> IPVersionVec) {
146146
std::stringstream ss;
147+
ss.imbue(std::locale::classic());
147148
bool amFirst = true;
148149
for (uint32_t ipVersion : IPVersionVec) {
149150
// if any device is not intelGPU, bail.

sycl/source/detail/kernel_program_cache.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ class KernelProgramCache {
362362

363363
int ImageId = CacheKey.first.second;
364364
std::stringstream DeviceList;
365+
DeviceList.imbue(
366+
std::locale::classic()); // avoid locale issues, like commas
365367
std::vector<unsigned char> SerializedObjVec = CacheKey.first.first;
366368

367369
// Convert spec constants to string. Spec constants are stored as

sycl/source/detail/program_manager/program_manager.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ static bool isDeviceBinaryTypeSupported(const ContextImplPtr &ContextImpl,
178178
return "unknown";
179179
}
180180

181+
// The string produced by this function might be localized, with commas and
182+
// periods inserted. Presently, it is used only for user facing error output.
181183
[[maybe_unused]] auto VecToString = [](auto &Vec) -> std::string {
182184
std::ostringstream Out;
183185
Out << "{";

0 commit comments

Comments
 (0)