]> granicus.if.org Git - icu/commitdiff
ICU-13207 Fix some resource leaks.
authorAndy Heninger <andy.heninger@gmail.com>
Tue, 13 Jun 2017 18:33:59 +0000 (18:33 +0000)
committerAndy Heninger <andy.heninger@gmail.com>
Tue, 13 Jun 2017 18:33:59 +0000 (18:33 +0000)
X-SVN-Rev: 40167

icu4c/source/common/putil.cpp
icu4c/source/common/uresbund.cpp
icu4c/source/i18n/vtzone.cpp

index 511ffb81ab683b5397126fc1390ba49833faebfa..6290b63cd5fc1fad22d5b333d43177919c3fa90c 100644 (file)
@@ -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
index 5a1dd0abcc4fecf79f71072261531810db7690bd..c51f7fdb64cc964c7750b8e1df7b540d8a4a96c1 100644 (file)
@@ -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;
                                 }
                             }
index b82327d53c6ed7b0343c59f474a48432a354f219..6ddcf4117d1d7af2763ffad358aa139c4435eea7 100644 (file)
@@ -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);
     }
 }