From: Andy Heninger Date: Tue, 13 Jun 2017 18:33:59 +0000 (+0000) Subject: ICU-13207 Fix some resource leaks. X-Git-Tag: milestone-60-0-1~34 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7d05feb7a6fd757121f22be5ad3325aa2dc67332;p=icu ICU-13207 Fix some resource leaks. X-SVN-Rev: 40167 --- diff --git a/icu4c/source/common/putil.cpp b/icu4c/source/common/putil.cpp index 511ffb81ab6..6290b63cd5f 100644 --- a/icu4c/source/common/putil.cpp +++ b/icu4c/source/common/putil.cpp @@ -939,30 +939,30 @@ static CharString *gSearchTZFileResult = NULL; * This function is not thread safe - it uses a global, gSearchTZFileResult, to hold its results. */ static char* searchForTZFile(const char* path, DefaultTZInfo* tzInfo) { - DIR* dirp = opendir(path); - DIR* subDirp = NULL; + DIR* dirp = NULL; struct dirent* dirEntry = NULL; - char* result = NULL; + UErrorCode status = U_ZERO_ERROR; + + /* Save the current path */ + CharString curpath(path, -1, status); + if (U_FAILURE(status)) { + goto cleanupAndReturn; + } + + dirp = opendir(path); if (dirp == NULL) { - return result; + goto cleanupAndReturn; } if (gSearchTZFileResult == NULL) { gSearchTZFileResult = new CharString; if (gSearchTZFileResult == NULL) { - return NULL; + goto cleanupAndReturn; } ucln_common_registerCleanup(UCLN_COMMON_PUTIL, putil_cleanup); } - /* Save the current path */ - UErrorCode status = U_ZERO_ERROR; - CharString curpath(path, -1, status); - if (U_FAILURE(status)) { - return NULL; - } - /* Check each entry in the directory. */ while((dirEntry = readdir(dirp)) != NULL) { const char* dirName = dirEntry->d_name; @@ -971,15 +971,16 @@ static char* searchForTZFile(const char* path, DefaultTZInfo* tzInfo) { CharString newpath(curpath, status); newpath.append(dirName, -1, status); if (U_FAILURE(status)) { - return NULL; + break; } + DIR* subDirp = NULL; if ((subDirp = opendir(newpath.data())) != NULL) { /* If this new path is a directory, make a recursive call with the newpath. */ closedir(subDirp); newpath.append('/', status); if (U_FAILURE(status)) { - return NULL; + break; } result = searchForTZFile(newpath.data(), tzInfo); /* @@ -1003,7 +1004,7 @@ static char* searchForTZFile(const char* path, DefaultTZInfo* tzInfo) { gSearchTZFileResult->clear(); gSearchTZFileResult->append(zoneid, -1, status); if (U_FAILURE(status)) { - return NULL; + break; } result = gSearchTZFileResult->data(); /* Get out after the first one found. */ @@ -1012,7 +1013,11 @@ static char* searchForTZFile(const char* path, DefaultTZInfo* tzInfo) { } } } - closedir(dirp); + + cleanupAndReturn: + if (dirp) { + closedir(dirp); + } return result; } #endif diff --git a/icu4c/source/common/uresbund.cpp b/icu4c/source/common/uresbund.cpp index 5a1dd0abcc4..c51f7fdb64c 100644 --- a/icu4c/source/common/uresbund.cpp +++ b/icu4c/source/common/uresbund.cpp @@ -1083,6 +1083,7 @@ static UResourceBundle *init_resb_result(const ResourceData *rdata, Resource r, pathBuf = (char *)uprv_malloc((uprv_strlen(keyPath)+1)*sizeof(char)); if(pathBuf == NULL) { *status = U_MEMORY_ALLOCATION_ERROR; + ures_close(mainRes); return NULL; } } diff --git a/icu4c/source/i18n/vtzone.cpp b/icu4c/source/i18n/vtzone.cpp index b82327d53c6..6ddcf4117d1 100644 --- a/icu4c/source/i18n/vtzone.cpp +++ b/icu4c/source/i18n/vtzone.cpp @@ -1747,26 +1747,16 @@ VTimeZone::write(VTZWriter& writer, UErrorCode& status) const { } } } else { - UVector *customProps = NULL; + UnicodeString icutzprop; + UVector customProps(nullptr, uhash_compareUnicodeString, status); if (olsonzid.length() > 0 && icutzver.length() > 0) { - customProps = new UVector(uprv_deleteUObject, uhash_compareUnicodeString, status); - if (U_FAILURE(status)) { - return; - } - UnicodeString *icutzprop = new UnicodeString(ICU_TZINFO_PROP); - icutzprop->append(olsonzid); - icutzprop->append((UChar)0x005B/*'['*/); - icutzprop->append(icutzver); - icutzprop->append((UChar)0x005D/*']'*/); - customProps->addElement(icutzprop, status); - if (U_FAILURE(status)) { - delete icutzprop; - delete customProps; - return; - } + icutzprop.append(olsonzid); + icutzprop.append(u'['); + icutzprop.append(icutzver); + icutzprop.append(u']'); + customProps.addElement(&icutzprop, status); } - writeZone(writer, *tz, customProps, status); - delete customProps; + writeZone(writer, *tz, &customProps, status); } }