From 5391094816708902eecded93a4ae9ee4a3f428f0 Mon Sep 17 00:00:00 2001 From: Vladimir Weinstein Date: Sat, 20 Jul 2002 04:44:57 +0000 Subject: [PATCH] ICU-1953 Collation code review X-SVN-Rev: 9271 --- icu4c/source/i18n/coleitr.cpp | 36 +++++++++++++++++++++-------------- icu4c/source/i18n/tblcoll.cpp | 11 +++++++---- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/icu4c/source/i18n/coleitr.cpp b/icu4c/source/i18n/coleitr.cpp index 2f20fdd052..9ba28fcbf1 100644 --- a/icu4c/source/i18n/coleitr.cpp +++ b/icu4c/source/i18n/coleitr.cpp @@ -178,7 +178,7 @@ void CollationElementIterator::setText(const UnicodeString& source, if (length > 0) { string = (UChar *)uprv_malloc(U_SIZEOF_UCHAR * length); /* test for NULL */ - if (string == 0) { + if (string == NULL) { status = U_MEMORY_ALLOCATION_ERROR; return; } @@ -187,7 +187,7 @@ void CollationElementIterator::setText(const UnicodeString& source, else { string = (UChar *)uprv_malloc(U_SIZEOF_UCHAR); /* test for NULL */ - if (string == 0) { + if (string == NULL) { status = U_MEMORY_ALLOCATION_ERROR; return; } @@ -212,7 +212,7 @@ void CollationElementIterator::setText(CharacterIterator& source, if (length == 0) { buffer = (UChar *)uprv_malloc(U_SIZEOF_UCHAR); /* test for NULL */ - if (buffer == 0) { + if (buffer == NULL) { status = U_MEMORY_ALLOCATION_ERROR; return; } @@ -221,7 +221,7 @@ void CollationElementIterator::setText(CharacterIterator& source, else { buffer = (UChar *)uprv_malloc(U_SIZEOF_UCHAR * length); /* test for NULL */ - if (buffer == 0) { + if (buffer == NULL) { status = U_MEMORY_ALLOCATION_ERROR; return; } @@ -276,7 +276,7 @@ CollationElementIterator::CollationElementIterator( if (length > 0) { string = (UChar *)uprv_malloc(U_SIZEOF_UCHAR * length); /* test for NULL */ - if (string == 0) { + if (string == NULL) { status = U_MEMORY_ALLOCATION_ERROR; return; } @@ -289,7 +289,7 @@ CollationElementIterator::CollationElementIterator( else { string = (UChar *)uprv_malloc(U_SIZEOF_UCHAR); /* test for NULL */ - if (string == 0) { + if (string == NULL) { status = U_MEMORY_ALLOCATION_ERROR; return; } @@ -341,7 +341,7 @@ CollationElementIterator::CollationElementIterator( if (length > 0) { buffer = (UChar *)uprv_malloc(U_SIZEOF_UCHAR * length); /* test for NULL */ - if (buffer == 0) { + if (buffer == NULL) { status = U_MEMORY_ALLOCATION_ERROR; return; } @@ -357,7 +357,7 @@ CollationElementIterator::CollationElementIterator( else { buffer = (UChar *)uprv_malloc(U_SIZEOF_UCHAR); /* test for NULL */ - if (buffer == 0) { + if (buffer == NULL) { status = U_MEMORY_ALLOCATION_ERROR; return; } @@ -405,8 +405,12 @@ const CollationElementIterator& CollationElementIterator::operator=( /* create a duplicate of string */ if (length > 0) { coliter->string = (UChar *)uprv_malloc(length * U_SIZEOF_UCHAR); - uprv_memcpy(coliter->string, othercoliter->string, - length * U_SIZEOF_UCHAR); + if(coliter->string != NULL) { + uprv_memcpy(coliter->string, othercoliter->string, + length * U_SIZEOF_UCHAR); + } else { // Error: couldn't allocate memory. No copying should be done + length = 0; + } } else { coliter->string = NULL; @@ -430,10 +434,14 @@ const CollationElementIterator& CollationElementIterator::operator=( } coliter->writableBuffer = (UChar *)uprv_malloc( wlength * U_SIZEOF_UCHAR); - uprv_memcpy(coliter->writableBuffer, - othercoliter->writableBuffer, - wlength * U_SIZEOF_UCHAR); - coliter->writableBufSize = wlength; + if(coliter->writableBuffer != NULL) { + uprv_memcpy(coliter->writableBuffer, + othercoliter->writableBuffer, + wlength * U_SIZEOF_UCHAR); + coliter->writableBufSize = wlength; + } else { // Error: couldn't allocate memory for writableBuffer + coliter->writableBufSize = 0; + } } } diff --git a/icu4c/source/i18n/tblcoll.cpp b/icu4c/source/i18n/tblcoll.cpp index 1907ab59d6..63df614d97 100644 --- a/icu4c/source/i18n/tblcoll.cpp +++ b/icu4c/source/i18n/tblcoll.cpp @@ -280,10 +280,13 @@ void RuleBasedCollator::getRules(UColRuleOption delta, UnicodeString &buffer) if (rulesize > 0) { UChar *rules = (UChar*) uprv_malloc( sizeof(UChar) * (rulesize) ); - - ucol_getRulesEx(ucollator, delta, rules, rulesize); - buffer.setTo(rules, rulesize); - uprv_free(rules); + if(rules != NULL) { + ucol_getRulesEx(ucollator, delta, rules, rulesize); + buffer.setTo(rules, rulesize); + uprv_free(rules); + } else { // couldn't allocate + buffer.remove(); + } } else { buffer.remove();