From 1725c5899ab7f602f8ef16e52242537820daae1d Mon Sep 17 00:00:00 2001 From: George Rhoten Date: Fri, 14 Dec 2007 10:17:12 +0000 Subject: [PATCH] ICU-6076 Fix some allocation issues. X-SVN-Rev: 23087 --- icu4c/source/common/rbbitblb.cpp | 6 +++--- icu4c/source/common/unicode/uniset.h | 4 ++-- icu4c/source/common/uniset.cpp | 22 +++++++++++++--------- icu4c/source/common/uvector.cpp | 17 +++++++++-------- icu4c/source/common/uvector.h | 2 +- icu4c/source/i18n/rbt_pars.cpp | 2 +- 6 files changed, 29 insertions(+), 24 deletions(-) diff --git a/icu4c/source/common/rbbitblb.cpp b/icu4c/source/common/rbbitblb.cpp index 78fd1c51d9c..719c86a9978 100644 --- a/icu4c/source/common/rbbitblb.cpp +++ b/icu4c/source/common/rbbitblb.cpp @@ -945,7 +945,7 @@ void RBBITableBuilder::setAdd(UVector *dest, UVector *source) { (void) dest->toArray(destBuff); (void) source->toArray(sourceBuff); - dest->setSize(sourceSize+destOriginalSize); + dest->setSize(sourceSize+destOriginalSize, *fStatus); while (sourceBuff < sourceLim && destBuff < destLim) { if (*destBuff == *sourceBuff) { @@ -970,7 +970,7 @@ void RBBITableBuilder::setAdd(UVector *dest, UVector *source) { dest->setElementAt(*sourceBuff++, di++); } - dest->setSize(di); + dest->setSize(di, *fStatus); if (destH) { uprv_free(destH); } @@ -1211,7 +1211,7 @@ RBBIStateDescriptor::RBBIStateDescriptor(int lastInputSymbol, UErrorCode *fStatu *fStatus = U_MEMORY_ALLOCATION_ERROR; return; } - fDtran->setSize(lastInputSymbol+1); // fDtran needs to be pre-sized. + fDtran->setSize(lastInputSymbol+1, *fStatus); // fDtran needs to be pre-sized. // It is indexed by input symbols, and will // hold the next state number for each // symbol. diff --git a/icu4c/source/common/unicode/uniset.h b/icu4c/source/common/unicode/uniset.h index 5ee2811f718..876009fbba9 100644 --- a/icu4c/source/common/unicode/uniset.h +++ b/icu4c/source/common/unicode/uniset.h @@ -1374,9 +1374,9 @@ private: // Implementation: Utility methods //---------------------------------------------------------------- - void ensureCapacity(int32_t newLen); + void ensureCapacity(int32_t newLen, UErrorCode& ec); - void ensureBufferCapacity(int32_t newLen); + void ensureBufferCapacity(int32_t newLen, UErrorCode& ec); void swapBuffers(void); diff --git a/icu4c/source/common/uniset.cpp b/icu4c/source/common/uniset.cpp index a09ed34ef61..aafb2373d7e 100644 --- a/icu4c/source/common/uniset.cpp +++ b/icu4c/source/common/uniset.cpp @@ -144,12 +144,13 @@ UnicodeSet::UnicodeSet() : len(1), capacity(1 + START_EXTRA), list(0), bmpSet(0), buffer(0), bufferCapacity(0), patLen(0), pat(NULL), strings(NULL), stringSpan(NULL) { + UErrorCode status = U_ZERO_ERROR; + allocateStrings(status); list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity); if(list!=NULL){ list[0] = UNICODESET_HIGH; } - UErrorCode status = U_ZERO_ERROR; - allocateStrings(status); + // else TODO: we should set this to bogus on malloc failure. _dbgct(this); } @@ -164,14 +165,14 @@ UnicodeSet::UnicodeSet(UChar32 start, UChar32 end) : len(1), capacity(1 + START_EXTRA), list(0), bmpSet(0), buffer(0), bufferCapacity(0), patLen(0), pat(NULL), strings(NULL), stringSpan(NULL) { + UErrorCode status = U_ZERO_ERROR; + allocateStrings(status); list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity); if(list!=NULL){ - UErrorCode status = U_ZERO_ERROR; - list[0] = UNICODESET_HIGH; - allocateStrings(status); complement(start, end); } + // else TODO: we should set this to bogus on malloc failure. _dbgct(this); } @@ -185,12 +186,13 @@ UnicodeSet::UnicodeSet(const UnicodeSet& o) : buffer(0), bufferCapacity(0), patLen(0), pat(NULL), strings(NULL), stringSpan(NULL) { + UErrorCode status = U_ZERO_ERROR; + allocateStrings(status); list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity); if(list!=NULL){ - UErrorCode status = U_ZERO_ERROR; - allocateStrings(status); *this = o; } + // else TODO: we should set this to bogus on malloc failure. _dbgct(this); } @@ -202,10 +204,11 @@ UnicodeSet::UnicodeSet(const UnicodeSet& o, UBool /* asThawed */) : buffer(0), bufferCapacity(0), patLen(0), pat(NULL), strings(NULL), stringSpan(NULL) { + UErrorCode status = U_ZERO_ERROR; + allocateStrings(status); + list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity); if(list!=NULL){ - UErrorCode status = U_ZERO_ERROR; - allocateStrings(status); // *this = o except for bmpSet and stringSpan len = o.len; uprv_memcpy(list, o.list, len*sizeof(UChar32)); @@ -214,6 +217,7 @@ UnicodeSet::UnicodeSet(const UnicodeSet& o, UBool /* asThawed */) : setPattern(UnicodeString(o.pat, o.patLen)); } } + // else TODO: we should set this to bogus on malloc failure. _dbgct(this); } diff --git a/icu4c/source/common/uvector.cpp b/icu4c/source/common/uvector.cpp index 2e9a37147f5..2c9efbc2084 100644 --- a/icu4c/source/common/uvector.cpp +++ b/icu4c/source/common/uvector.cpp @@ -93,12 +93,14 @@ UVector::~UVector() { */ void UVector::assign(const UVector& other, UTokenAssigner *assign, UErrorCode &ec) { if (ensureCapacity(other.count, ec)) { - setSize(other.count); - for (int32_t i=0; i count) { - UErrorCode ec = U_ZERO_ERROR; - if (!ensureCapacity(newSize, ec)) { + if (!ensureCapacity(newSize, status)) { return; } UHashTok empty; diff --git a/icu4c/source/common/uvector.h b/icu4c/source/common/uvector.h index 80a33a19156..a29b9c55419 100644 --- a/icu4c/source/common/uvector.h +++ b/icu4c/source/common/uvector.h @@ -195,7 +195,7 @@ public: * elements for i >= newSize. If newSize is larger, grow the * array, filling in new slots with NULL. */ - void setSize(int32_t newSize); + void setSize(int32_t newSize, UErrorCode &status); /** * Fill in the given array with all elements of this vector. diff --git a/icu4c/source/i18n/rbt_pars.cpp b/icu4c/source/i18n/rbt_pars.cpp index 83eccf54eaf..dd008704720 100644 --- a/icu4c/source/i18n/rbt_pars.cpp +++ b/icu4c/source/i18n/rbt_pars.cpp @@ -1508,7 +1508,7 @@ void TransliteratorParser::setSegmentObject(int32_t seg, StringMatcher* adopted, // and stored before segment i; be careful with the // vector handling here. if (segmentObjects.size() < seg) { - segmentObjects.setSize(seg); + segmentObjects.setSize(seg, status); } int32_t index = getSegmentStandin(seg, status) - curData->variablesBase; if (segmentObjects.elementAt(seg-1) != NULL ||