diff --git a/icu4c/source/common/locdispnames.cpp b/icu4c/source/common/locdispnames.cpp index 1d86c0f828..dff49ce626 100644 --- a/icu4c/source/common/locdispnames.cpp +++ b/icu4c/source/common/locdispnames.cpp @@ -662,7 +662,7 @@ uloc_getDisplayName(const char *locale, if ( p0 != NULL && p1 != NULL ) { /* The pattern is well formed */ if ( dest ) { int32_t destLen = 0; - UChar *result = (UChar *)uprv_malloc((length+1)*sizeof(UChar)); + UChar *result = (UChar *)uprv_malloc((length+1)*sizeof(UChar)); UChar *upos = (UChar *)dispLocPattern; u_strcpy(result,dest); dest[0] = 0; @@ -684,7 +684,8 @@ uloc_getDisplayName(const char *locale, destLen++; dest[destLen] = 0; /* Null terminate */ } - } + } + length = destLen; uprv_free(result); } } diff --git a/icu4c/source/test/cintltst/cloctst.c b/icu4c/source/test/cintltst/cloctst.c index a87644e18b..152688c517 100644 --- a/icu4c/source/test/cintltst/cloctst.c +++ b/icu4c/source/test/cintltst/cloctst.c @@ -240,6 +240,7 @@ void addLocaleTest(TestNode** root) TESTCASE(TestLikelySubtags); TESTCASE(TestToLanguageTag); TESTCASE(TestForLanguageTag); + TESTCASE(TestTrailingNull); } @@ -2119,9 +2120,28 @@ static void TestGetBaseName(void) { return; } } - } +static void TestTrailingNull(void) { + const char* localeId = "zh_Hans"; + UChar buffer[128]; /* sufficient for this test */ + int32_t len; + UErrorCode status = U_ZERO_ERROR; + int i; + + len = uloc_getDisplayName(localeId, localeId, buffer, 128, &status); + if (len > 128) { + log_err("buffer too small"); + return; + } + + for (i = 0; i < len; ++i) { + if (buffer[i] == 0) { + log_err("name contained null"); + return; + } + } +} /* Jitterbug 4115 */ static void TestDisplayNameWarning(void) { @@ -5536,4 +5556,3 @@ static void TestForLanguageTag(void) { } } } - diff --git a/icu4c/source/test/cintltst/cloctst.h b/icu4c/source/test/cintltst/cloctst.h index 8c5e73c538..a544f31a02 100644 --- a/icu4c/source/test/cintltst/cloctst.h +++ b/icu4c/source/test/cintltst/cloctst.h @@ -1,6 +1,6 @@ /******************************************************************** - * COPYRIGHT: - * Copyright (c) 1997-2009, International Business Machines Corporation and + * COPYRIGHT: + * Copyright (c) 1997-2010, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ /******************************************************************************** @@ -89,6 +89,8 @@ static void TestDisplayNames(void); static void TestGetBaseName(void); +static void TestTrailingNull(void); + static void TestGetLocale(void); /**