Skip to content

Commit 5991c93

Browse files
ICU-22890 MF2: Add ICU4C test for lone surrogates
Add a test to ICU4C for handling of lone surrogates. Incidentally fix uninitialized-memory bug in MessageFormatter (initialize `errors` to nullptr) Co-authored-by: Frank Tang <[email protected]>
1 parent 23edf9c commit 5991c93

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

icu4c/source/test/intltest/messageformat2test.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ TestMessageFormat2::runIndexedTest(int32_t index, UBool exec,
3131
TESTCASE_AUTO(testAPISimple);
3232
TESTCASE_AUTO(testDataModelAPI);
3333
TESTCASE_AUTO(testFormatterAPI);
34+
TESTCASE_AUTO(testHighLoneSurrogate);
35+
TESTCASE_AUTO(testLowLoneSurrogate);
3436
TESTCASE_AUTO(dataDrivenTests);
3537
TESTCASE_AUTO_END;
3638
}
@@ -337,6 +339,34 @@ void TestMessageFormat2::testAPICustomFunctions() {
337339
delete person;
338340
}
339341

342+
// ICU-22890 lone surrogate cause infinity loop
343+
void TestMessageFormat2::testHighLoneSurrogate() {
344+
IcuTestErrorCode errorCode(*this, "testHighLoneSurrogate");
345+
UParseError pe = { 0, 0, {0}, {0} };
346+
// Lone surrogate with only high surrogate
347+
UnicodeString loneSurrogate({0xda02, 0});
348+
icu::message2::MessageFormatter msgfmt1 =
349+
icu::message2::MessageFormatter::Builder(errorCode)
350+
.setPattern(loneSurrogate, pe, errorCode)
351+
.build(errorCode);
352+
UnicodeString result = msgfmt1.formatToString({}, errorCode);
353+
errorCode.expectErrorAndReset(U_MF_SYNTAX_ERROR, "testHighLoneSurrogate");
354+
}
355+
356+
// ICU-22890 lone surrogate cause infinity loop
357+
void TestMessageFormat2::testLowLoneSurrogate() {
358+
IcuTestErrorCode errorCode(*this, "testLowLoneSurrogate");
359+
UParseError pe = { 0, 0, {0}, {0} };
360+
// Lone surrogate with only low surrogate
361+
UnicodeString loneSurrogate({0xdc02, 0});
362+
icu::message2::MessageFormatter msgfmt2 =
363+
icu::message2::MessageFormatter::Builder(errorCode)
364+
.setPattern(loneSurrogate, pe, errorCode)
365+
.build(errorCode);
366+
UnicodeString result = msgfmt2.formatToString({}, errorCode);
367+
errorCode.expectErrorAndReset(U_MF_SYNTAX_ERROR, "testLowLoneSurrogate");
368+
}
369+
340370
void TestMessageFormat2::dataDrivenTests() {
341371
IcuTestErrorCode errorCode(*this, "jsonTests");
342372

icu4c/source/test/intltest/messageformat2test.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ class TestMessageFormat2: public IntlTest {
8989
void testMessageFormatDateTimeSkeleton(message2::TestCase::Builder&, IcuTestErrorCode&);
9090
void testMf1Behavior(message2::TestCase::Builder&, IcuTestErrorCode&);
9191

92+
void testHighLoneSurrogate(void);
93+
void testLowLoneSurrogate(void);
9294
}; // class TestMessageFormat2
9395

9496
U_NAMESPACE_BEGIN

0 commit comments

Comments
 (0)