Skip to content

Commit

Permalink
Merge pull request #4 from aheninger/ubsan-rbbi
Browse files Browse the repository at this point in the history
ICU-22224 Fix undefine behavior in rbbi constructor
  • Loading branch information
FrankYFTang authored Feb 27, 2023
2 parents f10fe15 + 3c8838c commit c9b65d7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
28 changes: 16 additions & 12 deletions icu4c/source/common/rbbi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RuleBasedBreakIterator)
* tables object that is passed in as a parameter.
*/
RuleBasedBreakIterator::RuleBasedBreakIterator(RBBIDataHeader* data, UErrorCode &status)
: RuleBasedBreakIterator(status)
: RuleBasedBreakIterator(&status)
{
fData = new RBBIDataWrapper(data, status); // status checked in constructor
if (U_FAILURE(status)) {return;}
Expand Down Expand Up @@ -101,7 +101,7 @@ RuleBasedBreakIterator::RuleBasedBreakIterator(UDataMemory* udm, UBool isPhraseB
RuleBasedBreakIterator::RuleBasedBreakIterator(const uint8_t *compiledRules,
uint32_t ruleLength,
UErrorCode &status)
: RuleBasedBreakIterator(status)
: RuleBasedBreakIterator(&status)
{
if (U_FAILURE(status)) {
return;
Expand Down Expand Up @@ -139,7 +139,7 @@ RuleBasedBreakIterator::RuleBasedBreakIterator(const uint8_t *compiledRules,
//
//-------------------------------------------------------------------------------
RuleBasedBreakIterator::RuleBasedBreakIterator(UDataMemory* udm, UErrorCode &status)
: RuleBasedBreakIterator(status)
: RuleBasedBreakIterator(&status)
{
fData = new RBBIDataWrapper(udm, status); // status checked in constructor
if (U_FAILURE(status)) {return;}
Expand Down Expand Up @@ -167,7 +167,7 @@ RuleBasedBreakIterator::RuleBasedBreakIterator(UDataMemory* udm, UErrorCode &sta
RuleBasedBreakIterator::RuleBasedBreakIterator( const UnicodeString &rules,
UParseError &parseError,
UErrorCode &status)
: RuleBasedBreakIterator(status)
: RuleBasedBreakIterator(&status)
{
if (U_FAILURE(status)) {return;}
RuleBasedBreakIterator *bi = (RuleBasedBreakIterator *)
Expand All @@ -189,21 +189,25 @@ RuleBasedBreakIterator::RuleBasedBreakIterator( const UnicodeString &rules,
// Used when creating a RuleBasedBreakIterator from a set
// of rules.
//-------------------------------------------------------------------------------
RuleBasedBreakIterator::RuleBasedBreakIterator() UPRV_NO_SANITIZE_UNDEFINED
: RuleBasedBreakIterator(fErrorCode)
RuleBasedBreakIterator::RuleBasedBreakIterator()
: RuleBasedBreakIterator(nullptr)
{
}

/**
* Simple Constructor with an error code.
* Handles common initialization for all other constructors.
*/
RuleBasedBreakIterator::RuleBasedBreakIterator(UErrorCode &status) {
utext_openUChars(&fText, nullptr, 0, &status);
LocalPointer<DictionaryCache> lpDictionaryCache(new DictionaryCache(this, status), status);
LocalPointer<BreakCache> lpBreakCache(new BreakCache(this, status), status);
if (U_FAILURE(status)) {
fErrorCode = status;
RuleBasedBreakIterator::RuleBasedBreakIterator(UErrorCode *status) {
UErrorCode ec = U_ZERO_ERROR;
if (status == nullptr) {
status = &ec;
}
utext_openUChars(&fText, nullptr, 0, status);
LocalPointer<DictionaryCache> lpDictionaryCache(new DictionaryCache(this, *status), *status);
LocalPointer<BreakCache> lpBreakCache(new BreakCache(this, *status), *status);
if (U_FAILURE(*status)) {
fErrorCode = *status;
return;
}
fDictionaryCache = lpDictionaryCache.orphan();
Expand Down
2 changes: 1 addition & 1 deletion icu4c/source/common/unicode/rbbi.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class U_COMMON_API RuleBasedBreakIterator /*U_FINAL*/ : public BreakIterator {
* Internally, handles common initialization for other constructors.
* @internal (private)
*/
RuleBasedBreakIterator(UErrorCode &status);
RuleBasedBreakIterator(UErrorCode *status);

public:

Expand Down

0 comments on commit c9b65d7

Please sign in to comment.