Skip to content

Commit 41afbe4

Browse files
committed
Adding another error case test for UTF-8 to UTF-16.
1 parent e470025 commit 41afbe4

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

Release/src/utilities/asyncrt_utils.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,14 +286,15 @@ utf16string __cdecl conversions::utf8_to_utf16(const std::string &s)
286286
}
287287
else
288288
{
289+
if ((*src & BIT8) != 0 && (*src & BIT7) == 0)
290+
{
291+
throw std::range_error("UTF-8 string character can never start with 10xxxxxx");
292+
}
293+
289294
unsigned char numContBytes = 0;
290295
uint32_t codePoint;
291296
if ((*src & BIT6) == 0) // 2 byte character, 0x80 to 0x7FF
292297
{
293-
if ((*src & BIT8) != 0 && (*src & BIT7) == 0)
294-
{
295-
throw std::range_error("UTF-8 string character can never start with 10xxxxxx");
296-
}
297298
codePoint = *src & LOW_5BITS;
298299
numContBytes = 1;
299300
}
@@ -362,8 +363,8 @@ std::string __cdecl conversions::utf16_to_utf8(const utf16string &w)
362363
// Check for high surrogate.
363364
if (*src >= H_SURROGATE_START && *src <= H_SURROGATE_END)
364365
{
365-
const auto highSurrogate = *src;
366-
if (++src == w.end())
366+
const auto highSurrogate = *src++;
367+
if (src == w.end())
367368
{
368369
throw std::range_error("UTF-16 string is missing low surrogate");
369370
}

Release/tests/functional/utils/strings.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ TEST(utf8_to_utf16_errors)
256256
input.push_back(128u); // 10000000
257257
input.push_back(128u); // 10000000
258258
VERIFY_THROWS(utility::conversions::utf8_to_utf16(input), std::range_error);
259+
input.clear();
260+
input.push_back(191u); // 10111111
261+
input.push_back(128u); // 10000000
262+
VERIFY_THROWS(utility::conversions::utf8_to_utf16(input), std::range_error);
259263
}
260264

261265
TEST(latin1_to_utf16)

0 commit comments

Comments
 (0)