Skip to content

Commit

Permalink
ICU-22609 Fix nulldef w/ bogus locale in DateFormat::creaet*
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankYFTang committed Dec 15, 2023
1 parent 5cf5ec1 commit 3057c8e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
8 changes: 7 additions & 1 deletion icu4c/source/i18n/datefmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,9 +519,15 @@ DateFormat::createInstanceForSkeleton(
//----------------------------------------------------------------------

DateFormat* U_EXPORT2
DateFormat::create(EStyle timeStyle, EStyle dateStyle, const Locale& locale)
DateFormat::create(EStyle timeStyle, EStyle dateStyle, const Locale& inLocale)
{
UErrorCode status = U_ZERO_ERROR;
Locale locale(inLocale);
// If the locale is bogus, fallback to use default locale first.
if (locale.isBogus()) {
locale = Locale::getDefault();
}

#if U_PLATFORM_USES_ONLY_WIN32_API
char buffer[8];
int32_t count = locale.getKeywordValue("compat", buffer, sizeof(buffer), status);
Expand Down
5 changes: 4 additions & 1 deletion icu4c/source/i18n/smpdtfmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,10 @@ void SimpleDateFormat::construct(EStyle timeStyle,
{
// called by several constructors to load pattern data from the resources
if (U_FAILURE(status)) return;
if (locale.isBogus()) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return;
}

// We will need the calendar to know what type of symbols to load.
initializeCalendar(nullptr, locale, status);
Expand Down Expand Up @@ -728,7 +732,6 @@ void SimpleDateFormat::construct(EStyle timeStyle,
if (locale.getKeywordValue("rg", nullptr, 0, dummyErr1) > 0 || locale.getKeywordValue("hours", nullptr, 0, dummyErr2) > 0) {
hasRgOrHcSubtag = true;
}

const char* baseLocID = locale.getBaseName();
if (baseLocID[0]!=0 && uprv_strcmp(baseLocID,"und")!=0) {
UErrorCode useStatus = U_ZERO_ERROR;
Expand Down
15 changes: 15 additions & 0 deletions icu4c/source/test/cintltst/cdattst.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ static void TestNarrowQuarters(void);
static void TestExtraneousCharacters(void);
static void TestParseTooStrict(void);
static void TestHourCycle(void);
static void TestLocaleNameCrash(void);

void addDateForTest(TestNode** root);

Expand All @@ -76,6 +77,7 @@ void addDateForTest(TestNode** root)
TESTCASE(TestExtraneousCharacters);
TESTCASE(TestParseTooStrict);
TESTCASE(TestHourCycle);
TESTCASE(TestLocaleNameCrash);
}
/* Testing the DateFormat API */
static void TestDateFormat()
Expand Down Expand Up @@ -2129,4 +2131,17 @@ static void TestHourCycle(void) {
}
}

static void TestLocaleNameCrash(void) {
UErrorCode status = U_ZERO_ERROR;
UDateFormat icudf;

icudf = udat_open(UDAT_MEDIUM, UDAT_NONE, "notalanguage", NULL, 0, NULL, 0, &status);
if ( U_SUCCESS(status) ) {
log_verbose("Success: did not crash on udat_open(locale=\"notalanguage\")\n");
} else {
log_err("FAIL: didn't crash on udat_open(locale=\"notalanguage\"), but got %s.\n", u_errorName(status));
}
udat_close(icudf);
}

#endif /* #if !UCONFIG_NO_FORMATTING */
9 changes: 9 additions & 0 deletions icu4c/source/test/intltest/dtfmttst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ void DateFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &nam
TESTCASE_AUTO(TestNumericFieldStrictParse);
TESTCASE_AUTO(TestHourCycle);
TESTCASE_AUTO(TestHCInLocale);
TESTCASE_AUTO(TestBogusLocale);

TESTCASE_AUTO_END;
}
Expand Down Expand Up @@ -5876,6 +5877,14 @@ void DateFormatTest::TestHourCycle() {
}
}

void DateFormatTest::TestBogusLocale() {
IcuTestErrorCode status(*this, "TestBogusLocale");
LocalPointer<DateFormat> df;

df.adoptInstead(DateFormat::createDateTimeInstance(DateFormat::kNone, DateFormat::kMedium,
Locale("notalanguage")));
}

void DateFormatTest::TestHCInLocale() {
IcuTestErrorCode status(*this, "TestHCInLocale");
LocalPointer<Calendar> midnight(Calendar::createInstance(status));
Expand Down
1 change: 1 addition & 0 deletions icu4c/source/test/intltest/dtfmttst.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ class DateFormatTest: public CalendarTimeZoneTest {
void TestNumericFieldStrictParse();
void TestHourCycle();
void TestHCInLocale();
void TestBogusLocale();

private:
UBool showParse(DateFormat &format, const UnicodeString &formattedString);
Expand Down

0 comments on commit 3057c8e

Please sign in to comment.