diff --git a/icu4c/source/common/unicode/locid.h b/icu4c/source/common/unicode/locid.h index 7e410e53c741..b474a744344a 100644 --- a/icu4c/source/common/unicode/locid.h +++ b/icu4c/source/common/unicode/locid.h @@ -1121,7 +1121,7 @@ template inline void Locale::getKeywords(OutputIterator iterator, UErrorCode& status) const { LocalPointer keys(createKeywords(status)); - if (U_FAILURE(status)) { + if (U_FAILURE(status) || keys.isNull()) { return; } for (;;) { @@ -1138,7 +1138,7 @@ template inline void Locale::getUnicodeKeywords(OutputIterator iterator, UErrorCode& status) const { LocalPointer keys(createUnicodeKeywords(status)); - if (U_FAILURE(status)) { + if (U_FAILURE(status) || keys.isNull()) { return; } for (;;) { diff --git a/icu4c/source/test/intltest/loctest.cpp b/icu4c/source/test/intltest/loctest.cpp index 8db52c6b48d6..ee946569c688 100644 --- a/icu4c/source/test/intltest/loctest.cpp +++ b/icu4c/source/test/intltest/loctest.cpp @@ -233,7 +233,9 @@ void LocaleTest::runIndexedTest( int32_t index, UBool exec, const char* &name, c TESTCASE_AUTO(TestCreateUnicodeKeywords); TESTCASE_AUTO(TestKeywordVariantParsing); TESTCASE_AUTO(TestCreateKeywordSet); + TESTCASE_AUTO(TestCreateKeywordSetEmpty); TESTCASE_AUTO(TestCreateUnicodeKeywordSet); + TESTCASE_AUTO(TestCreateUnicodeKeywordSetEmpty); TESTCASE_AUTO(TestGetKeywordValueStdString); TESTCASE_AUTO(TestGetUnicodeKeywordValueStdString); TESTCASE_AUTO(TestSetKeywordValue); @@ -1912,6 +1914,21 @@ LocaleTest::TestCreateKeywordSet(void) { result.find("collation") != result.end()); } +void +LocaleTest::TestCreateKeywordSetEmpty(void) { + IcuTestErrorCode status(*this, "TestCreateKeywordSetEmpty()"); + + static const Locale l("de"); + + std::set result; + l.getKeywords( + std::insert_iterator(result, result.begin()), + status); + status.errIfFailureAndReset("\"%s\"", l.getName()); + + assertEquals("set::size()", 0, static_cast(result.size())); +} + void LocaleTest::TestCreateUnicodeKeywordSet(void) { IcuTestErrorCode status(*this, "TestCreateUnicodeKeywordSet()"); @@ -1931,6 +1948,21 @@ LocaleTest::TestCreateUnicodeKeywordSet(void) { result.find("co") != result.end()); } +void +LocaleTest::TestCreateUnicodeKeywordSetEmpty(void) { + IcuTestErrorCode status(*this, "TestCreateUnicodeKeywordSetEmpty()"); + + static const Locale l("de"); + + std::set result; + l.getUnicodeKeywords( + std::insert_iterator(result, result.begin()), + status); + status.errIfFailureAndReset("\"%s\"", l.getName()); + + assertEquals("set::size()", 0, static_cast(result.size())); +} + void LocaleTest::TestGetKeywordValueStdString(void) { IcuTestErrorCode status(*this, "TestGetKeywordValueStdString()"); diff --git a/icu4c/source/test/intltest/loctest.h b/icu4c/source/test/intltest/loctest.h index 85da54fee5bf..2771419ffce5 100644 --- a/icu4c/source/test/intltest/loctest.h +++ b/icu4c/source/test/intltest/loctest.h @@ -79,7 +79,9 @@ class LocaleTest: public IntlTest { /* Test getting keyword values */ void TestKeywordVariantParsing(void); void TestCreateKeywordSet(void); + void TestCreateKeywordSetEmpty(void); void TestCreateUnicodeKeywordSet(void); + void TestCreateUnicodeKeywordSetEmpty(void); void TestGetKeywordValueStdString(void); void TestGetUnicodeKeywordValueStdString(void);