Skip to content

Commit 42365d0

Browse files
authored
Merge pull request #5506 from thomasspriggs/tas/fix_windows_jbmc_unicode
Fix JBMC crash on windows in `NondetEnumOpaqueReturn` test
2 parents 95aa3ad + a67ca97 commit 42365d0

File tree

3 files changed

+289
-3
lines changed

3 files changed

+289
-3
lines changed

src/util/string_utils.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,20 @@ std::string escape_non_alnum(const std::string &to_escape)
156156
std::ostringstream escaped;
157157
for(auto &ch : to_escape)
158158
{
159+
// `ch` may have a negative value in the case of utf-8 encodings of
160+
// characters above unicode code point 127. The following line maps these
161+
// negative values to positive values in the 128-255 range, using a
162+
// `static_cast`. This is neccessary in order to avoid undefined behaviour
163+
// in `isalnum`. The positive values are then stored in an integer using a
164+
// widening initialisation so that the stream insertion operator prints them
165+
// as numbers rather than characters.
166+
const int uch{static_cast<unsigned char>(ch)};
159167
if(ch == '_')
160168
escaped << "__";
161-
else if(isalnum(ch))
169+
else if(isalnum(uch))
162170
escaped << ch;
163171
else
164-
escaped << '_' << std::hex << std::setfill('0') << std::setw(2)
165-
<< (unsigned int)ch;
172+
escaped << '_' << std::hex << std::setfill('0') << std::setw(2) << uch;
166173
}
167174
return escaped.str();
168175
}

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ SRC += analyses/ai/ai.cpp \
119119
util/string2int.cpp \
120120
util/structured_data.cpp \
121121
util/string_utils/capitalize.cpp \
122+
util/string_utils/escape_non_alnum.cpp \
122123
util/string_utils/join_string.cpp \
123124
util/string_utils/split_string.cpp \
124125
util/string_utils/strip_string.cpp \

0 commit comments

Comments
 (0)