ICU-6132 Add null pointer check before using in translit and transreg.

X-SVN-Rev: 23245
This commit is contained in:
Michael Ow 2008-01-16 18:01:51 +00:00
parent 9cb51f2e5d
commit aa76d931cf
2 changed files with 124 additions and 56 deletions
icu4c/source/i18n

View file

@ -1,6 +1,6 @@
/*
**********************************************************************
* Copyright (C) 1999-2007, International Business Machines
* Copyright (C) 1999-2008, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
* Date Name Description
@ -948,10 +948,12 @@ Transliterator::createInstance(const UnicodeString& ID,
else {
t = (Transliterator*)list.elementAt(0);
}
t->setID(canonID);
if (globalFilter != NULL) {
t->adoptFilter(globalFilter);
// Check null pointer
if (t != NULL) {
t->setID(canonID);
if (globalFilter != NULL) {
t->adoptFilter(globalFilter);
}
}
return t;
}
@ -1103,15 +1105,26 @@ Transliterator::createFromRules(const UnicodeString& ID,
}
if (!parser.dataVector.isEmpty()) {
TransliterationRuleData* data = (TransliterationRuleData*)parser.dataVector.orphanElementAt(0);
transliterators.addElement(
new RuleBasedTransliterator(UnicodeString(CompoundTransliterator::PASS_STRING) + (passNumber++),
data, TRUE), status);
RuleBasedTransliterator* temprbt = new RuleBasedTransliterator(UnicodeString(CompoundTransliterator::PASS_STRING) + (passNumber++),
data, TRUE);
// Check if NULL before adding it to transliterators to avoid future usage of NULL pointer.
if (temprbt == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
return t;
}
transliterators.addElement(temprbt, status);
}
}
t = new CompoundTransliterator(transliterators, passNumber - 1, parseError, status);
t->setID(ID);
t->adoptFilter(parser.orphanCompoundFilter());
// Null pointer check
if (t != NULL) {
t->setID(ID);
t->adoptFilter(parser.orphanCompoundFilter());
}
}
if (t == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
}
return t;
}
@ -1164,21 +1177,25 @@ const Transliterator& Transliterator::getElement(int32_t index, UErrorCode& ec)
UnicodeSet& Transliterator::getSourceSet(UnicodeSet& result) const {
handleGetSourceSet(result);
if (filter != NULL) {
UnicodeSet* filterSet;
UBool deleteFilterSet = FALSE;
// Most, but not all filters will be UnicodeSets. Optimize for
// the high-runner case.
if (filter->getDynamicClassID() == UnicodeSet::getStaticClassID()) {
filterSet = (UnicodeSet*) filter;
} else {
filterSet = new UnicodeSet();
deleteFilterSet = TRUE;
filter->addMatchSetTo(*filterSet);
}
result.retainAll(*filterSet);
if (deleteFilterSet) {
delete filterSet;
}
UnicodeSet* filterSet;
UBool deleteFilterSet = FALSE;
// Most, but not all filters will be UnicodeSets. Optimize for
// the high-runner case.
if (filter->getDynamicClassID() == UnicodeSet::getStaticClassID()) {
filterSet = (UnicodeSet*) filter;
} else {
filterSet = new UnicodeSet();
// Check null pointer
if (filterSet == NULL) {
return result;
}
deleteFilterSet = TRUE;
filter->addMatchSetTo(*filterSet);
}
result.retainAll(*filterSet);
if (deleteFilterSet) {
delete filterSet;
}
}
return result;
}
@ -1533,13 +1550,37 @@ UBool Transliterator::initializeRegistry() {
// Manually add prototypes that the system knows about to the
// cache. This is how new non-rule-based transliterators are
// added to the system.
// This is to allow for null pointer check
NullTransliterator* tempNullTranslit = new NullTransliterator();
LowercaseTransliterator* tempLowercaseTranslit = new LowercaseTransliterator();
UppercaseTransliterator* tempUppercaseTranslit = new UppercaseTransliterator();
TitlecaseTransliterator* tempTitlecaseTranslit = new TitlecaseTransliterator();
UnicodeNameTransliterator* tempUnicodeTranslit = new UnicodeNameTransliterator();
NameUnicodeTransliterator* tempNameUnicodeTranslit = new NameUnicodeTransliterator();
// Check for null pointers
if (tempNullTranslit == NULL || tempLowercaseTranslit == NULL || tempUppercaseTranslit == NULL ||
tempTitlecaseTranslit == NULL || tempUnicodeTranslit == NULL || tempNameUnicodeTranslit == NULL) {
delete tempNullTranslit;
delete tempLowercaseTranslit;
delete tempUppercaseTranslit;
delete tempTitlecaseTranslit;
delete tempUnicodeTranslit;
delete tempNameUnicodeTranslit;
// Since there was an error, remove registry
delete registry;
registry = NULL;
return 0;
}
registry->put(new NullTransliterator(), TRUE);
registry->put(new LowercaseTransliterator(), TRUE);
registry->put(new UppercaseTransliterator(), TRUE);
registry->put(new TitlecaseTransliterator(), TRUE);
registry->put(new UnicodeNameTransliterator(), TRUE);
registry->put(new NameUnicodeTransliterator(), TRUE);
registry->put(tempNullTranslit, TRUE);
registry->put(tempLowercaseTranslit, TRUE);
registry->put(tempUppercaseTranslit, TRUE);
registry->put(tempTitlecaseTranslit, TRUE);
registry->put(tempUnicodeTranslit, TRUE);
registry->put(tempNameUnicodeTranslit, TRUE);
RemoveTransliterator::registerIDs(); // Must be within mutex
EscapeTransliterator::registerIDs();

View file

@ -1,6 +1,6 @@
/*
**********************************************************************
* Copyright (c) 2001-2006, International Business Machines
* Copyright (c) 2001-2008, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
* Date Name Description
@ -612,8 +612,11 @@ Transliterator* TransliteratorRegistry::reget(const UnicodeString& ID,
void TransliteratorRegistry::put(Transliterator* adoptedProto,
UBool visible) {
Entry *entry = new Entry();
entry->adoptPrototype(adoptedProto);
registerEntry(adoptedProto->getID(), entry, visible);
// Null pointer check
if (entry != NULL) {
entry->adoptPrototype(adoptedProto);
registerEntry(adoptedProto->getID(), entry, visible);
}
}
void TransliteratorRegistry::put(const UnicodeString& ID,
@ -621,8 +624,11 @@ void TransliteratorRegistry::put(const UnicodeString& ID,
Transliterator::Token context,
UBool visible) {
Entry *entry = new Entry();
entry->setFactory(factory, context);
registerEntry(ID, entry, visible);
// Null pointer check
if (entry != NULL) {
entry->setFactory(factory, context);
registerEntry(ID, entry, visible);
}
}
void TransliteratorRegistry::put(const UnicodeString& ID,
@ -631,15 +637,18 @@ void TransliteratorRegistry::put(const UnicodeString& ID,
UBool readonlyResourceAlias,
UBool visible) {
Entry *entry = new Entry();
entry->entryType = (dir == UTRANS_FORWARD) ? Entry::RULES_FORWARD
: Entry::RULES_REVERSE;
if (readonlyResourceAlias) {
entry->stringArg.setTo(TRUE, resourceName.getBuffer(), -1);
// Null pointer check
if (entry != NULL) {
entry->entryType = (dir == UTRANS_FORWARD) ? Entry::RULES_FORWARD
: Entry::RULES_REVERSE;
if (readonlyResourceAlias) {
entry->stringArg.setTo(TRUE, resourceName.getBuffer(), -1);
}
else {
entry->stringArg = resourceName;
}
registerEntry(ID, entry, visible);
}
else {
entry->stringArg = resourceName;
}
registerEntry(ID, entry, visible);
}
void TransliteratorRegistry::put(const UnicodeString& ID,
@ -647,14 +656,17 @@ void TransliteratorRegistry::put(const UnicodeString& ID,
UBool readonlyAliasAlias,
UBool visible) {
Entry *entry = new Entry();
entry->entryType = Entry::ALIAS;
if (readonlyAliasAlias) {
entry->stringArg.setTo(TRUE, alias.getBuffer(), -1);
// Null pointer check
if (entry != NULL) {
entry->entryType = Entry::ALIAS;
if (readonlyAliasAlias) {
entry->stringArg.setTo(TRUE, alias.getBuffer(), -1);
}
else {
entry->stringArg = alias;
}
registerEntry(ID, entry, visible);
}
else {
entry->stringArg = alias;
}
registerEntry(ID, entry, visible);
}
void TransliteratorRegistry::remove(const UnicodeString& ID) {
@ -885,9 +897,12 @@ void TransliteratorRegistry::registerEntry(const UnicodeString& ID,
registerSTV(source, target, variant);
if (!availableIDs.contains((void*) &ID)) {
UnicodeString *newID = (UnicodeString *)ID.clone();
// NUL-terminate the ID string
newID->getTerminatedBuffer();
availableIDs.addElement(newID, status);
// Check to make sure newID was created.
if (newID != NULL) {
// NUL-terminate the ID string
newID->getTerminatedBuffer();
availableIDs.addElement(newID, status);
}
}
} else {
removeSTV(source, target, variant);
@ -929,10 +944,17 @@ void TransliteratorRegistry::registerSTV(const UnicodeString& source,
// We add the variant string. If it is the special "no variant"
// string, that is, the empty string, we add it at position zero.
if (!variants->contains((void*) &variant)) {
UnicodeString *tempus; // Used for null pointer check.
if (variant.length() > 0) {
variants->addElement(new UnicodeString(variant), status);
tempus = new UnicodeString(variant);
if (tempus != NULL) {
variants->addElement(tempus, status);
}
} else {
variants->insertElementAt(new UnicodeString(NO_VARIANT), 0, status);
tempus = new UnicodeString(NO_VARIANT) ;
if (tempus != NULL) {
variants->insertElementAt(tempus, 0, status);
}
}
}
}
@ -1236,6 +1258,11 @@ Transliterator* TransliteratorRegistry::instantiateEntry(const UnicodeString& ID
case Entry::COMPOUND_RBT:
{
UVector* rbts = new UVector(status);
// Check for null pointer
if (rbts == NULL) {
status = U_MEMORY_ALLOCATION_ERROR;
return NULL;
}
int32_t passNumber = 1;
for (int32_t i = 0; U_SUCCESS(status) && i < entry->u.dataVector->size(); i++) {
Transliterator* t = new RuleBasedTransliterator(UnicodeString(CompoundTransliterator::PASS_STRING) + (passNumber++),