]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
Fix a memory leak in the unicode library.
authorOliver Kurth <okurth@vmware.com>
Tue, 30 Apr 2019 20:24:25 +0000 (13:24 -0700)
committerOliver Kurth <okurth@vmware.com>
Tue, 30 Apr 2019 20:24:25 +0000 (13:24 -0700)
Ensure that allocated strings are freed before returning a failure.

The ASSERTs have never been known to fire; a warning in a obj
build will help with debugging. The warning should "never" happen.

open-vm-tools/lib/unicode/unicodeICU.c

index b63932e0f39341db83a0cf1e6466dec539666515..b9b2dbb45274fba1745c8b83d873e6cd2902079a 100644 (file)
@@ -1,5 +1,5 @@
 /*********************************************************
- * Copyright (C) 2008-2016 VMware, Inc. All rights reserved.
+ * Copyright (C) 2008-2019 VMware, Inc. All rights reserved.
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as published
@@ -275,7 +275,7 @@ Unicode_ToLower(const char *str,    // IN
     */
 
    // Most lower-case operations don't change the length of the string.
-   utf8Dest = (char *)Util_SafeMalloc(destCapacity);
+   utf8Dest = Util_SafeMalloc(destCapacity);
 
    caseMap = ucasemap_open(locale, 0, &status);
    if (U_FAILURE(status)) {
@@ -295,7 +295,7 @@ Unicode_ToLower(const char *str,    // IN
 
    // If we need a bigger buffer, then reallocate and retry.
    destCapacity = destLen + 1;
-   utf8Dest = (char *)Util_SafeRealloc(utf8Dest, destCapacity);
+   utf8Dest = Util_SafeRealloc(utf8Dest, destCapacity);
 
    status = U_ZERO_ERROR;
    destLen = ucasemap_utf8ToLower(caseMap,
@@ -311,8 +311,9 @@ Unicode_ToLower(const char *str,    // IN
    if (U_SUCCESS(status) && status != U_STRING_NOT_TERMINATED_WARNING) {
       result = utf8Dest;
    } else {
-      ASSERT(U_SUCCESS(status));
-      ASSERT(status != U_STRING_NOT_TERMINATED_WARNING);
+      DEBUG_ONLY(Warning("%s: Invalid UTF-8 string detected.\n",
+                         __FUNCTION__));
+      free(utf8Dest);
    }
 
    return result;
@@ -356,7 +357,7 @@ Unicode_ToUpper(const char *str,    // IN
    char *result = NULL;
 
    // Most upper-case operations don't change the length of the string.
-   utf8Dest = (char *)Util_SafeMalloc(destCapacity);
+   utf8Dest = Util_SafeMalloc(destCapacity);
 
    caseMap = ucasemap_open(locale, 0, &status);
    if (U_FAILURE(status)) {
@@ -376,7 +377,7 @@ Unicode_ToUpper(const char *str,    // IN
 
    // If we need a bigger buffer, then reallocate and retry.
    destCapacity = destLen + 1;
-   utf8Dest = (char *)Util_SafeRealloc(utf8Dest, destCapacity);
+   utf8Dest = Util_SafeRealloc(utf8Dest, destCapacity);
 
    status = U_ZERO_ERROR;
    destLen = ucasemap_utf8ToUpper(caseMap,
@@ -392,13 +393,15 @@ Unicode_ToUpper(const char *str,    // IN
    if (U_SUCCESS(status) && status != U_STRING_NOT_TERMINATED_WARNING) {
       result = utf8Dest;
    } else {
-      ASSERT(U_SUCCESS(status));
-      ASSERT(status != U_STRING_NOT_TERMINATED_WARNING);
+      DEBUG_ONLY(Warning("%s: Invalid UTF-8 string detected.\n",
+                         __FUNCTION__));
+      free(utf8Dest);
    }
 
    return result;
 }
 
+
 /*
  * "ucasemap_utf8ToTitle" is not in version 3.6 of the ICU library,
  * which appears to be the default on many systems...
@@ -447,7 +450,7 @@ Unicode_ToTitle(const char *str,    // IN
    char *result = NULL;
 
    // Most title-case operations don't change the length of the string.
-   utf8Dest = (char *)Util_SafeMalloc(destCapacity);
+   utf8Dest = Util_SafeMalloc(destCapacity);
 
    caseMap = ucasemap_open(locale, 0, &status);
    if (U_FAILURE(status)) {
@@ -467,7 +470,7 @@ Unicode_ToTitle(const char *str,    // IN
 
    // If we need a bigger buffer, then reallocate and retry.
    destCapacity = destLen + 1;
-   utf8Dest = (char *)Util_SafeRealloc(utf8Dest, destCapacity);
+   utf8Dest = Util_SafeRealloc(utf8Dest, destCapacity);
 
    status = U_ZERO_ERROR;
    destLen = ucasemap_utf8ToTitle(caseMap,
@@ -483,8 +486,9 @@ Unicode_ToTitle(const char *str,    // IN
    if (U_SUCCESS(status) && status != U_STRING_NOT_TERMINATED_WARNING) {
       result = utf8Dest;
    } else {
-      ASSERT(U_SUCCESS(status));
-      ASSERT(status != U_STRING_NOT_TERMINATED_WARNING);
+      DEBUG_ONLY(Warning("%s: Invalid UTF-8 string detected.\n",
+                         __FUNCTION__));
+      free(utf8Dest);
    }
 
    return result;