Skip to content

Commit

Permalink
add error handling for invalid utf8
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Dec 29, 2024
1 parent 5605bfd commit da9a4f2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Engine/utility/textcodec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ std::u16string TextCodec::toUtf16(std::string_view inS) {
for(size_t i=0; i<inS.size();) {
uint32_t cp = 0;
size_t l = Detail::utf8ToCodepoint(&s[i],cp);
if(l==0)
throw std::range_error("invalid utf-8"); // similar to stl

if(cp > 0xFFFF)
sz+=2; else
Expand Down
5 changes: 5 additions & 0 deletions Tests/tests/textcodec_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,8 @@ TEST(main,TextCodec_UTF8_4) {

TextCodec_Base(u8,u16);
}

TEST(main,TextCodec_UTF8_invalid) {
std::string u8 = "\340\214\244\276m\177\000\000\200\002\000\000h\001\000";
EXPECT_ANY_THROW(TextCodec::toUtf16(u8));
}

0 comments on commit da9a4f2

Please sign in to comment.