Skip to content

Commit

Permalink
ICU-22581 Fix RBBI leakage
Browse files Browse the repository at this point in the history
Duplicate variable references in the rule should not cause leakage
  • Loading branch information
FrankYFTang committed Dec 8, 2023
1 parent 665d9db commit 73f972f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions icu4c/source/common/rbbiscan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ UBool RBBIRuleScanner::doParseActions(int32_t action)

// Terminate expression, leaves expression parse tree rooted in TOS node.
fixOpStack(RBBINode::precStart);
if (U_FAILURE(*fRB->fStatus)) {
break;
}

RBBINode *startExprNode = fNodeStack[fNodeStackPtr-2];
RBBINode *varRefNode = fNodeStack[fNodeStackPtr-1];
Expand All @@ -312,6 +315,11 @@ UBool RBBIRuleScanner::doParseActions(int32_t action)
UErrorCode t = *fRB->fStatus;
*fRB->fStatus = U_ZERO_ERROR;
error(t);
// When adding $variableRef to the symbol table fail, Delete
// both nodes because deleting varRefNode will not delete
// RHSExprNode internally.
delete RHSExprNode;
delete varRefNode;
}

// Clean up the stack.
Expand Down
9 changes: 9 additions & 0 deletions icu4c/source/test/intltest/rbbitst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ void RBBITest::runIndexedTest( int32_t index, UBool exec, const char* &name, cha
TESTCASE_AUTO(TestRandomAccess);
TESTCASE_AUTO(TestExternalBreakEngineWithFakeTaiLe);
TESTCASE_AUTO(TestExternalBreakEngineWithFakeYue);
TESTCASE_AUTO(TestBug22581);
TESTCASE_AUTO(TestBug22584);

#if U_ENABLE_TRACING
Expand Down Expand Up @@ -5873,4 +5874,12 @@ void RBBITest::TestBug22584() {
RuleBasedBreakIterator bi(ruleStr, pe, ec);
}

void RBBITest::TestBug22581() {
// Test duplicate variable setting will not leak the rule compilation
UnicodeString ruleStr = u"$foo=[abc]; $foo=[xyz]; $foo;";
UParseError pe {};
UErrorCode ec {U_ZERO_ERROR};

RuleBasedBreakIterator bi(ruleStr, pe, ec);
}
#endif // #if !UCONFIG_NO_BREAK_ITERATION
1 change: 1 addition & 0 deletions icu4c/source/test/intltest/rbbitst.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class RBBITest: public IntlTest {
void TestRandomAccess();
void TestExternalBreakEngineWithFakeTaiLe();
void TestExternalBreakEngineWithFakeYue();
void TestBug22581();
void TestBug22584();

#if U_ENABLE_TRACING
Expand Down

0 comments on commit 73f972f

Please sign in to comment.