Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ void Stream::StreamInUtf16() const {
// Deal with the next UTF-16 unit
if (chLow < 0xD800 || chLow >= 0xE000) {
// Easiest case: queue the codepoint and return
QueueUnicodeCodepoint(m_readahead, ch);
QueueUnicodeCodepoint(m_readahead, chLow);
return;
}
// Start the loop over with the new high surrogate
Expand Down
23 changes: 23 additions & 0 deletions test/integration/encoding_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,28 @@ TEST_F(EncodingTest, UTF32BE_BOM) {
SetUpEncoding(&EncodeToUtf32BE, true);
Run();
}

// An orphaned UTF-16 high surrogate must decode to a single replacement
// character, and the code unit after it must still be decoded. Previously the
// high surrogate was re-emitted as an ill-formed UTF-8 sequence and the
// following character was dropped.
TEST(Utf16SurrogateTest, OrphanHighSurrogateKeepsFollowingChar) {
auto put = [](std::string& s, int unit) {
s += Byte(unit & 0xFF);
s += Byte((unit >> 8) & 0xFF);
};

std::string input;
put(input, 0xFEFF); // UTF-16LE BOM
put(input, 'x');
put(input, ':');
put(input, ' ');
put(input, 0xD800); // high surrogate with no trailing low surrogate
put(input, 'A');

std::stringstream stream(input);
Node node = Load(stream);
EXPECT_EQ(node["x"].as<std::string>(), "\xEF\xBF\xBD" "A");
}
}
}