ICU-21001 Fixing problems found by running valgrind.
This makes fixes in order to run the icu4c tests (intltest, cintltst,
iotest, and icuinfo) cleanly under valgrind with --leak-check=full.
diff --git a/icu4c/source/common/ucurr.cpp b/icu4c/source/common/ucurr.cpp
index dfee3bd..5eacc4a 100644
--- a/icu4c/source/common/ucurr.cpp
+++ b/icu4c/source/common/ucurr.cpp
@@ -866,7 +866,7 @@
*total_currency_name_count = 0;
*total_currency_symbol_count = 0;
const UChar* s = NULL;
- char locale[ULOC_FULLNAME_CAPACITY];
+ char locale[ULOC_FULLNAME_CAPACITY] = "";
uprv_strcpy(locale, loc);
const icu::Hashtable *currencySymbolsEquiv = getCurrSymbolsEquiv();
for (;;) {
@@ -941,7 +941,7 @@
// Look up the Currencies resource for the given locale.
UErrorCode ec2 = U_ZERO_ERROR;
- char loc[ULOC_FULLNAME_CAPACITY];
+ char loc[ULOC_FULLNAME_CAPACITY] = "";
uloc_getName(locale, loc, sizeof(loc), &ec2);
if (U_FAILURE(ec2) || ec2 == U_STRING_NOT_TERMINATED_WARNING) {
ec = U_ILLEGAL_ARGUMENT_ERROR;
diff --git a/icu4c/source/common/unames.cpp b/icu4c/source/common/unames.cpp
index cde8b5a..5776058 100644
--- a/icu4c/source/common/unames.cpp
+++ b/icu4c/source/common/unames.cpp
@@ -1519,7 +1519,8 @@
u_charFromName(UCharNameChoice nameChoice,
const char *name,
UErrorCode *pErrorCode) {
- char upper[120], lower[120];
+ char upper[120] = {0};
+ char lower[120] = {0};
FindName findName;
AlgorithmicRange *algRange;
uint32_t *p;
diff --git a/icu4c/source/common/uresbund.cpp b/icu4c/source/common/uresbund.cpp
index 6c0e760..97df4a8 100644
--- a/icu4c/source/common/uresbund.cpp
+++ b/icu4c/source/common/uresbund.cpp
@@ -2611,8 +2611,8 @@
char defVal[1024] = ""; /* default value for given locale */
char defLoc[1024] = ""; /* default value for given locale */
char base[1024] = ""; /* base locale */
- char found[1024];
- char parent[1024];
+ char found[1024] = "";
+ char parent[1024] = "";
char full[1024] = "";
UResourceBundle bund1, bund2;
UResourceBundle *res = NULL;
diff --git a/icu4c/source/common/uscript.cpp b/icu4c/source/common/uscript.cpp
index 98528c1..f8bd7e7 100644
--- a/icu4c/source/common/uscript.cpp
+++ b/icu4c/source/common/uscript.cpp
@@ -58,8 +58,8 @@
getCodesFromLocale(const char *locale,
UScriptCode *scripts, int32_t capacity, UErrorCode *err) {
UErrorCode internalErrorCode = U_ZERO_ERROR;
- char lang[8];
- char script[8];
+ char lang[8] = {0};
+ char script[8] = {0};
int32_t scriptLength;
if(U_FAILURE(*err)) { return 0; }
// Multi-script languages, equivalent to the LocaleScript data
diff --git a/icu4c/source/i18n/calendar.cpp b/icu4c/source/i18n/calendar.cpp
index c043bb3..981f09c 100644
--- a/icu4c/source/i18n/calendar.cpp
+++ b/icu4c/source/i18n/calendar.cpp
@@ -958,7 +958,7 @@
#endif
c->setWeekData(aLocale, c->getType(), success); // set the correct locale (this was an indirected calendar)
- char keyword[ULOC_FULLNAME_CAPACITY];
+ char keyword[ULOC_FULLNAME_CAPACITY] = "";
UErrorCode tmpStatus = U_ZERO_ERROR;
l.getKeywordValue("calendar", keyword, ULOC_FULLNAME_CAPACITY, tmpStatus);
if (U_SUCCESS(tmpStatus) && uprv_strcmp(keyword, "iso8601") == 0) {
diff --git a/icu4c/source/i18n/dtptngen.cpp b/icu4c/source/i18n/dtptngen.cpp
index af92b78..4d4a3e5 100644
--- a/icu4c/source/i18n/dtptngen.cpp
+++ b/icu4c/source/i18n/dtptngen.cpp
@@ -324,6 +324,7 @@
DateTimePatternGenerator::DateTimePatternGenerator(UErrorCode &status) :
skipMatcher(nullptr),
fAvailableFormatKeyHash(nullptr),
+ fDefaultHourFormatChar(0),
internalErrorCode(U_ZERO_ERROR)
{
fp = new FormatParser();
@@ -338,6 +339,7 @@
DateTimePatternGenerator::DateTimePatternGenerator(const Locale& locale, UErrorCode &status) :
skipMatcher(nullptr),
fAvailableFormatKeyHash(nullptr),
+ fDefaultHourFormatChar(0),
internalErrorCode(U_ZERO_ERROR)
{
fp = new FormatParser();
@@ -356,6 +358,7 @@
UObject(),
skipMatcher(nullptr),
fAvailableFormatKeyHash(nullptr),
+ fDefaultHourFormatChar(0),
internalErrorCode(U_ZERO_ERROR)
{
fp = new FormatParser();
@@ -1639,7 +1642,7 @@
&& (typeValue!= UDATPG_YEAR_FIELD || reqFieldChar==CAP_Y))
? reqFieldChar
: field.charAt(0);
- if (typeValue == UDATPG_HOUR_FIELD) {
+ if (typeValue == UDATPG_HOUR_FIELD && fDefaultHourFormatChar != 0) {
// The adjustment here is required to match spec (https://www.unicode.org/reports/tr35/tr35-dates.html#dfst-hour).
// It is necessary to match the hour-cycle preferred by the Locale.
// Given that, we need to do the following adjustments:
diff --git a/icu4c/source/i18n/gender.cpp b/icu4c/source/i18n/gender.cpp
index 32ddbf9..dc5def6 100644
--- a/icu4c/source/i18n/gender.cpp
+++ b/icu4c/source/i18n/gender.cpp
@@ -160,7 +160,7 @@
if (s == NULL) {
return &gObjs[NEUTRAL];
}
- char type_str[256];
+ char type_str[256] = "";
u_UCharsToChars(s, type_str, resLen + 1);
if (uprv_strcmp(type_str, gNeutralStr) == 0) {
return &gObjs[NEUTRAL];
diff --git a/icu4c/source/i18n/name2uni.cpp b/icu4c/source/i18n/name2uni.cpp
index 52bab80..dcf8d85 100644
--- a/icu4c/source/i18n/name2uni.cpp
+++ b/icu4c/source/i18n/name2uni.cpp
@@ -190,6 +190,7 @@
}
if (uprv_isInvariantUString(name.getBuffer(), len)) {
+ cbuf[0] = 0;
name.extract(0, len, cbuf, maxLen, US_INV);
UErrorCode status = U_ZERO_ERROR;
diff --git a/icu4c/source/i18n/numsys.cpp b/icu4c/source/i18n/numsys.cpp
index ee530e8..62d555a 100644
--- a/icu4c/source/i18n/numsys.cpp
+++ b/icu4c/source/i18n/numsys.cpp
@@ -118,7 +118,7 @@
UBool nsResolved = TRUE;
UBool usingFallback = FALSE;
- char buffer[ULOC_KEYWORDS_CAPACITY];
+ char buffer[ULOC_KEYWORDS_CAPACITY] = "";
int32_t count = inLocale.getKeywordValue("numbers", buffer, sizeof(buffer), status);
if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING) {
// the "numbers" keyword exceeds ULOC_KEYWORDS_CAPACITY; ignore and use default.
diff --git a/icu4c/source/test/intltest/dadrcal.cpp b/icu4c/source/test/intltest/dadrcal.cpp
index 08c9cec..c74f387 100644
--- a/icu4c/source/test/intltest/dadrcal.cpp
+++ b/icu4c/source/test/intltest/dadrcal.cpp
@@ -448,7 +448,7 @@
//Calendar *cal= NULL;
//const UChar *arguments= NULL;
//int32_t argLen = 0;
- char testType[256];
+ char testType[256] = "";
const DataMap *settings= NULL;
//const UChar *type= NULL;
UErrorCode status = U_ZERO_ERROR;
diff --git a/icu4c/source/test/intltest/dadrfmt.cpp b/icu4c/source/test/intltest/dadrfmt.cpp
index a382bd5..bff134a 100644
--- a/icu4c/source/test/intltest/dadrfmt.cpp
+++ b/icu4c/source/test/intltest/dadrfmt.cpp
@@ -330,7 +330,7 @@
//Format *cal= NULL;
//const UChar *arguments= NULL;
//int32_t argLen = 0;
- char testType[256];
+ char testType[256] = "";
const DataMap *settings= NULL;
//const UChar *type= NULL;
UErrorCode status = U_ZERO_ERROR;
diff --git a/icu4c/source/test/intltest/tztest.cpp b/icu4c/source/test/intltest/tztest.cpp
index 9346610..17492a7 100644
--- a/icu4c/source/test/intltest/tztest.cpp
+++ b/icu4c/source/test/intltest/tztest.cpp
@@ -537,7 +537,7 @@
const UnicodeString *id1, *id2;
UnicodeString canonicalID;
UBool isSystemID;
- char region[4];
+ char region[4] = {0};
int32_t zoneCount;
any = canonical = canonicalLoc = any_US = canonical_US = canonicalLoc_US = any_W5 = any_CA_W5 = any_US_E14 = NULL;
diff --git a/icu4c/source/tools/icuinfo/icuinfo.cpp b/icu4c/source/tools/icuinfo/icuinfo.cpp
index fee17cf..9348259 100644
--- a/icu4c/source/tools/icuinfo/icuinfo.cpp
+++ b/icu4c/source/tools/icuinfo/icuinfo.cpp
@@ -58,6 +58,12 @@
}
}
+static void do_cleanup() {
+ if (icuInitted) {
+ u_cleanup();
+ icuInitted = FALSE;
+ }
+}
void cmd_millis()
{
@@ -295,5 +301,7 @@
cmd_version(FALSE, errorCode); /* at least print the version # */
}
+ do_cleanup();
+
return U_FAILURE(errorCode);
}