From cd9a595ff6a84501a8760e5032b5552f9c6ca8fd Mon Sep 17 00:00:00 2001 From: Alan Liu Date: Mon, 18 Mar 2002 21:25:01 +0000 Subject: [PATCH] ICU-1779 avoid malloc(0) X-SVN-Rev: 8091 --- icu4c/source/common/unistr.cpp | 3 +++ icu4c/source/common/uvector.cpp | 4 ++++ icu4c/source/i18n/rbt_set.cpp | 3 ++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/icu4c/source/common/unistr.cpp b/icu4c/source/common/unistr.cpp index d81a55ab662..e6818828825 100644 --- a/icu4c/source/common/unistr.cpp +++ b/icu4c/source/common/unistr.cpp @@ -1364,6 +1364,9 @@ UnicodeString::handleReplaceBetween(int32_t start, */ void UnicodeString::copy(int32_t start, int32_t limit, int32_t dest) { + if (limit <= start) { + return; // Nothing to do; avoid bogus malloc call + } UChar* text = (UChar*) uprv_malloc( sizeof(UChar) * (limit - start) ); extractBetween(start, limit, text, 0); insert(dest, text, 0, limit - start); diff --git a/icu4c/source/common/uvector.cpp b/icu4c/source/common/uvector.cpp index 68f248dced2..bd09e2c1ab7 100644 --- a/icu4c/source/common/uvector.cpp +++ b/icu4c/source/common/uvector.cpp @@ -32,6 +32,10 @@ UVector::UVector(int32_t initialCapacity, UErrorCode &status) : deleter(0), comparer(0) { + // Fix bogus initialCapacity values; avoid malloc(0) + if (initialCapacity < 1) { + initialCapacity = 1; + } _init(initialCapacity, status); } diff --git a/icu4c/source/i18n/rbt_set.cpp b/icu4c/source/i18n/rbt_set.cpp index 99cfadd5640..ecdd04fada4 100644 --- a/icu4c/source/i18n/rbt_set.cpp +++ b/icu4c/source/i18n/rbt_set.cpp @@ -276,8 +276,9 @@ void TransliterationRuleSet::freeze(UParseError& parseError,UErrorCode& status) } /* Precompute the index values. This saves a LOT of time. + * Be careful not to call malloc(0). */ - int16_t* indexValue = (int16_t*) uprv_malloc( sizeof(int16_t) * n ); + int16_t* indexValue = (int16_t*) uprv_malloc( sizeof(int16_t) * (n > 0 ? n : 1) ); for (j=0; jelementAt(j); indexValue[j] = r->getIndexValue();