From 4c6887c581ee5cb62e39815ff70c591ab293fcbf Mon Sep 17 00:00:00 2001 From: Peter Edberg Date: Tue, 26 Jan 2010 07:21:21 +0000 Subject: [PATCH] ICU-7295 ucal_open crash if locale==NULL && caltype==UCAL_GREGORIAN X-SVN-Rev: 27414 --- icu4c/source/i18n/ucal.cpp | 3 +++ icu4c/source/test/cintltst/ccaltst.c | 12 ++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/icu4c/source/i18n/ucal.cpp b/icu4c/source/i18n/ucal.cpp index d47dacb361e..8d9c96d7d86 100644 --- a/icu4c/source/i18n/ucal.cpp +++ b/icu4c/source/i18n/ucal.cpp @@ -135,6 +135,9 @@ ucal_open( const UChar* zoneID, if ( caltype == UCAL_GREGORIAN ) { char localeBuf[ULOC_LOCALE_IDENTIFIER_CAPACITY]; + if ( locale == NULL ) { + locale = uloc_getDefault(); + } uprv_strncpy(localeBuf, locale, ULOC_LOCALE_IDENTIFIER_CAPACITY); uloc_setKeywordValue("calendar", "gregorian", localeBuf, ULOC_LOCALE_IDENTIFIER_CAPACITY, status); if (U_FAILURE(*status)) { diff --git a/icu4c/source/test/cintltst/ccaltst.c b/icu4c/source/test/cintltst/ccaltst.c index 76b6328239b..41ae1d29433 100644 --- a/icu4c/source/test/cintltst/ccaltst.c +++ b/icu4c/source/test/cintltst/ccaltst.c @@ -71,6 +71,9 @@ static const UCalGetTypeTest ucalGetTypeTests[] = { { "ja_JP@calendar=japanese", UCAL_DEFAULT, "japanese" }, { "th_TH", UCAL_GREGORIAN, "gregorian" }, { "th_TH", UCAL_DEFAULT, "buddhist" }, + { "ja_JP@calendar=japanese", UCAL_GREGORIAN, "gregorian" }, + { "", UCAL_GREGORIAN, "gregorian" }, + { NULL, UCAL_GREGORIAN, "gregorian" }, { NULL, 0, NULL } /* terminator */ }; @@ -415,22 +418,23 @@ static void TestCalendar() ucal_close(calfrclone); /*testing ucal_getType, and ucal_open with UCAL_GREGORIAN*/ - for (ucalGetTypeTestPtr = ucalGetTypeTests; ucalGetTypeTestPtr->locale != NULL; ++ucalGetTypeTestPtr) { + for (ucalGetTypeTestPtr = ucalGetTypeTests; ucalGetTypeTestPtr->expectedResult != NULL; ++ucalGetTypeTestPtr) { + const char * localeToDisplay = (ucalGetTypeTestPtr->locale != NULL)? ucalGetTypeTestPtr->locale: ""; status = U_ZERO_ERROR; caldef = ucal_open(NULL, 0, ucalGetTypeTestPtr->locale, ucalGetTypeTestPtr->calType, &status); if ( U_SUCCESS(status) ) { const char * calType = ucal_getType(caldef, &status); if ( U_SUCCESS(status) && calType != NULL ) { if ( strcmp( calType, ucalGetTypeTestPtr->expectedResult ) != 0 ) { - log_err("FAIL: ucal_open %s type %d does not return %s calendar\n", ucalGetTypeTestPtr->locale, + log_err("FAIL: ucal_open %s type %d does not return %s calendar\n", localeToDisplay, ucalGetTypeTestPtr->calType, ucalGetTypeTestPtr->expectedResult); } } else { - log_err("FAIL: ucal_open %s type %d, then ucal_getType fails\n", ucalGetTypeTestPtr->locale, ucalGetTypeTestPtr->calType); + log_err("FAIL: ucal_open %s type %d, then ucal_getType fails\n", localeToDisplay, ucalGetTypeTestPtr->calType); } ucal_close(caldef); } else { - log_err("FAIL: ucal_open %s type %d fails\n", ucalGetTypeTestPtr->locale, ucalGetTypeTestPtr->calType); + log_err("FAIL: ucal_open %s type %d fails\n", localeToDisplay, ucalGetTypeTestPtr->calType); } }