From 3daa4c96d4230d6c3d7cbca8cc1c262000a37871 Mon Sep 17 00:00:00 2001 From: George Rhoten Date: Fri, 8 Feb 2008 08:21:32 +0000 Subject: [PATCH] ICU-6132 Recover from allocation errors more gracefully. X-SVN-Rev: 23398 --- icu4c/source/common/ucase.c | 21 +++++++++++++++------ icu4c/source/common/ucase.h | 4 +++- icu4c/source/common/ucnv_bld.c | 5 +++-- icu4c/source/i18n/transreg.cpp | 6 ++++-- icu4c/source/i18n/zonemeta.cpp | 1 + icu4c/source/test/cintltst/cucdtst.c | 6 ++---- icu4c/source/test/cintltst/nucnvtst.c | 7 ++++--- 7 files changed, 32 insertions(+), 18 deletions(-) diff --git a/icu4c/source/common/ucase.c b/icu4c/source/common/ucase.c index 2352176be15..063af297f07 100644 --- a/icu4c/source/common/ucase.c +++ b/icu4c/source/common/ucase.c @@ -1,7 +1,7 @@ /* ******************************************************************************* * -* Copyright (C) 2004-2007, International Business Machines +* Copyright (C) 2004-2008, International Business Machines * Corporation and others. All Rights Reserved. * ******************************************************************************* @@ -17,6 +17,8 @@ * Much code moved here (and modified) from uchar.c. */ +#define UCASE_HARDCODE_DATA 1 + #include "unicode/utypes.h" #include "unicode/uset.h" #include "unicode/udata.h" /* UDataInfo */ @@ -41,8 +43,6 @@ struct UCaseProps { /* data loading etc. -------------------------------------------------------- */ -#define UCASE_HARDCODE_DATA 1 - #if UCASE_HARDCODE_DATA /* ucase_props_data.c is machine-generated by gencase --csource */ @@ -205,23 +205,24 @@ ucase_close(UCaseProps *csp) { /* UCaseProps singleton ----------------------------------------------------- */ -static UCaseProps *gCsp=NULL, *gCspDummy=NULL; #if !UCASE_HARDCODE_DATA +static UCaseProps *gCsp=NULL; +static UCaseProps *gCspDummy=NULL; static UErrorCode gErrorCode=U_ZERO_ERROR; static int8_t gHaveData=0; #endif +#if !UCASE_HARDCODE_DATA static UBool U_CALLCONV ucase_cleanup(void) { ucase_close(gCsp); gCsp=NULL; ucase_close(gCspDummy); gCspDummy=NULL; -#if !UCASE_HARDCODE_DATA gErrorCode=U_ZERO_ERROR; gHaveData=0; -#endif return TRUE; } +#endif U_CAPI const UCaseProps * U_EXPORT2 ucase_getSingleton(UErrorCode *pErrorCode) { @@ -271,6 +272,7 @@ ucase_getSingleton(UErrorCode *pErrorCode) { #endif } +#if !UCASE_HARDCODE_DATA U_CAPI const UCaseProps * U_EXPORT2 ucase_getDummy(UErrorCode *pErrorCode) { UCaseProps *csp; @@ -322,6 +324,7 @@ ucase_getDummy(UErrorCode *pErrorCode) { return gCspDummy; } } +#endif /* set of property starts for UnicodeSet ------------------------------------ */ @@ -1488,6 +1491,7 @@ ucase_toFullFolding(const UCaseProps *csp, UChar32 c, /* case mapping properties API ---------------------------------------------- */ /* get the UCaseProps singleton, or else its dummy, once and for all */ +#if !UCASE_HARDCODE_DATA static const UCaseProps * getCaseProps() { /* @@ -1511,6 +1515,7 @@ getCaseProps() { return csp; } +#endif /* * In ICU 3.0, most Unicode properties were loaded from uprops.icu. @@ -1539,7 +1544,11 @@ getCaseProps() { * Other API implementations get the singleton themselves * (with mutexing), store it in the service object, and report errors. */ +#if !UCASE_HARDCODE_DATA #define GET_CASE_PROPS() (gCsp!=NULL ? gCsp : getCaseProps()) +#else +#define GET_CASE_PROPS() &ucase_props_singleton +#endif /* public API (see uchar.h) */ diff --git a/icu4c/source/common/ucase.h b/icu4c/source/common/ucase.h index 1c29482d8fc..5ace376d107 100644 --- a/icu4c/source/common/ucase.h +++ b/icu4c/source/common/ucase.h @@ -1,7 +1,7 @@ /* ******************************************************************************* * -* Copyright (C) 2004-2007, International Business Machines +* Copyright (C) 2004-2008, International Business Machines * Corporation and others. All Rights Reserved. * ******************************************************************************* @@ -44,6 +44,7 @@ ucase_close(UCaseProps *csp); U_CAPI const UCaseProps * U_EXPORT2 ucase_getSingleton(UErrorCode *pErrorCode); +#if defined(UCASE_HARDCODE_DATA) && !UCASE_HARDCODE_DATA /** * Get a singleton dummy object, one that works with no real data. * This can be used when the real data is not available. @@ -51,6 +52,7 @@ ucase_getSingleton(UErrorCode *pErrorCode); */ U_CAPI const UCaseProps * U_EXPORT2 ucase_getDummy(UErrorCode *pErrorCode); +#endif U_CAPI int32_t U_EXPORT2 diff --git a/icu4c/source/common/ucnv_bld.c b/icu4c/source/common/ucnv_bld.c index a9e52b3df03..3a6dbf9f0b6 100644 --- a/icu4c/source/common/ucnv_bld.c +++ b/icu4c/source/common/ucnv_bld.c @@ -977,6 +977,9 @@ ucnv_flushCache () /* Close the default converter without creating a new one so that everything will be flushed. */ ucnv_close(u_getDefaultConverter(&status)); + /* Flush now because we may not have any shared data in the hash. */ + ucnv_flushAvailableConverterCache(); + /*if shared data hasn't even been lazy evaluated yet * return 0 */ @@ -1029,8 +1032,6 @@ ucnv_flushCache () UTRACE_DATA1(UTRACE_INFO, "ucnv_flushCache() exits with %d converters remaining", remaining); - ucnv_flushAvailableConverterCache(); - UTRACE_EXIT_VALUE(tableDeletedNum); return tableDeletedNum; } diff --git a/icu4c/source/i18n/transreg.cpp b/icu4c/source/i18n/transreg.cpp index c0a7c5cbe07..c6274b4872b 100644 --- a/icu4c/source/i18n/transreg.cpp +++ b/icu4c/source/i18n/transreg.cpp @@ -1257,7 +1257,7 @@ Transliterator* TransliteratorRegistry::instantiateEntry(const UnicodeString& ID return t; case Entry::COMPOUND_RBT: { - UVector* rbts = new UVector(status); + UVector* rbts = new UVector(entry->u.dataVector->size(), status); // Check for null pointer if (rbts == NULL) { status = U_MEMORY_ALLOCATION_ERROR; @@ -1272,8 +1272,10 @@ Transliterator* TransliteratorRegistry::instantiateEntry(const UnicodeString& ID else rbts->addElement(t, status); } - if (U_FAILURE(status)) + if (U_FAILURE(status)) { + delete rbts; return 0; + } aliasReturn = new TransliteratorAlias(ID, entry->stringArg, rbts, entry->compoundFilter); } if (aliasReturn == 0) { diff --git a/icu4c/source/i18n/zonemeta.cpp b/icu4c/source/i18n/zonemeta.cpp index 15fbf797c3e..42cbe476c6d 100644 --- a/icu4c/source/i18n/zonemeta.cpp +++ b/icu4c/source/i18n/zonemeta.cpp @@ -634,6 +634,7 @@ error_cleanup: if (metaToOlson != NULL) { delete metaToOlson; } + metaToOlson = NULL; goto normal_cleanup; } diff --git a/icu4c/source/test/cintltst/cucdtst.c b/icu4c/source/test/cintltst/cucdtst.c index f132d12e6f2..d8772fd3531 100644 --- a/icu4c/source/test/cintltst/cucdtst.c +++ b/icu4c/source/test/cintltst/cucdtst.c @@ -1,6 +1,6 @@ /******************************************************************** * COPYRIGHT: - * Copyright (c) 1997-2007, International Business Machines Corporation and + * Copyright (c) 1997-2008, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ /******************************************************************************* @@ -3054,11 +3054,9 @@ static void TestUCase() { #if !HARDCODED_DATA_4497 UDataMemory *pData; UCaseProps *csp; -#endif const UCaseProps *ccsp; UErrorCode errorCode; -#if !HARDCODED_DATA_4497 /* coverage for ucase_openBinary() */ errorCode=U_ZERO_ERROR; pData=udata_open(NULL, UCASE_DATA_TYPE, UCASE_DATA_NAME, &errorCode); @@ -3082,7 +3080,6 @@ static void TestUCase() { ucase_close(csp); udata_close(pData); -#endif /* coverage for ucase_getDummy() */ errorCode=U_ZERO_ERROR; @@ -3090,6 +3087,7 @@ static void TestUCase() { if(ucase_tolower(ccsp, 0x41)!=0x41) { log_err("ucase_tolower(dummy, A)!=A\n"); } +#endif } /* API coverage for ubidi_props.c */ diff --git a/icu4c/source/test/cintltst/nucnvtst.c b/icu4c/source/test/cintltst/nucnvtst.c index 0e7999d44da..397eb38aeb0 100644 --- a/icu4c/source/test/cintltst/nucnvtst.c +++ b/icu4c/source/test/cintltst/nucnvtst.c @@ -1,6 +1,6 @@ /******************************************************************** * COPYRIGHT: - * Copyright (c) 1997-2007, International Business Machines Corporation and + * Copyright (c) 1997-2008, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ /******************************************************************************* @@ -5214,12 +5214,13 @@ static void TestJitterbug981(){ int numNeeded=0; utf8cnv = ucnv_open ("utf8", &status); if(U_FAILURE(status)){ - log_err("Could not open UTF-8 converter. Error: %s", u_errorName(status)); + log_err("Could not open UTF-8 converter. Error: %s\n", u_errorName(status)); return; } myCollator = ucol_open("zh", &status); if(U_FAILURE(status)){ - log_err("Could not open collator for zh locale. Error: %s", u_errorName(status)); + log_err("Could not open collator for zh locale. Error: %s\n", u_errorName(status)); + ucnv_close(utf8cnv); return; }