Skip to content

Commit

Permalink
ICU-22626 Fix leakage when 2 '=' in PluralRules
Browse files Browse the repository at this point in the history
See #2782
  • Loading branch information
FrankYFTang committed Jan 12, 2024
1 parent dc7014f commit 509405c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions icu4c/source/i18n/plurrule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,11 @@ PluralRuleParser::parse(const UnicodeString& ruleData, PluralRules *prules, UErr
case tEqual:
{
U_ASSERT(curAndConstraint != nullptr);
if (curAndConstraint->rangeList != nullptr) {
// Already get a '='.
status = U_UNEXPECTED_TOKEN;
break;
}
LocalPointer<UVector32> newRangeList(new UVector32(status), status);
if (U_FAILURE(status)) {
break;
Expand Down
11 changes: 11 additions & 0 deletions icu4c/source/test/intltest/plurults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ void PluralRulesTest::runIndexedTest( int32_t index, UBool exec, const char* &na
TESTCASE_AUTO(testFixedDecimal);
TESTCASE_AUTO(testSelectTrailingZeros);
TESTCASE_AUTO(testLocaleExtension);
TESTCASE_AUTO(testDoubleEqualSign);
TESTCASE_AUTO_END;
}

Expand Down Expand Up @@ -1638,6 +1639,16 @@ void PluralRulesTest::compareLocaleResults(const char* loc1, const char* loc2, c
}
}

void PluralRulesTest::testDoubleEqualSign() {
IcuTestErrorCode errorCode(*this, "testDoubleEqualSign");

// ICU-22626
// Two '=' in the rul should not leak.
LocalPointer<PluralRules> rules(
PluralRules::createRules(u"e:c=2=", errorCode), errorCode);
errorCode.expectErrorAndReset(U_UNEXPECTED_TOKEN);
}

void PluralRulesTest::testLocaleExtension() {
IcuTestErrorCode errorCode(*this, "testLocaleExtension");
LocalPointer<PluralRules> rules(PluralRules::forLocale("pt@calendar=gregorian", errorCode));
Expand Down
1 change: 1 addition & 0 deletions icu4c/source/test/intltest/plurults.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class PluralRulesTest : public IntlTest {
void testFixedDecimal();
void testSelectTrailingZeros();
void testLocaleExtension();
void testDoubleEqualSign();

void assertRuleValue(const UnicodeString& rule, double expected);
void assertRuleKeyValue(const UnicodeString& rule, const UnicodeString& key,
Expand Down

0 comments on commit 509405c

Please sign in to comment.