From: Oliver Kurth Date: Wed, 1 Apr 2020 18:31:36 +0000 (-0700) Subject: lib/unicode/unicodeSimpleTypes.c: fix leak on exit X-Git-Tag: stable-11.2.0~274 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ded2a20ac11ef4f9340847d19ae2a14431a4702f;p=thirdparty%2Fopen-vm-tools.git lib/unicode/unicodeSimpleTypes.c: fix leak on exit The hashtable used for encoding name -> IANA table index lookups was not getting freed. Also, HashTable_AllocOnce() incorrectly instructs the hashtable to clear its clientData, which contains an integer index and not a pointer, using "free". --- diff --git a/open-vm-tools/lib/unicode/unicodeSimpleTypes.c b/open-vm-tools/lib/unicode/unicodeSimpleTypes.c index 5ace167e2..59811093d 100644 --- a/open-vm-tools/lib/unicode/unicodeSimpleTypes.c +++ b/open-vm-tools/lib/unicode/unicodeSimpleTypes.c @@ -1,5 +1,5 @@ /********************************************************* - * Copyright (C) 2007-2019 VMware, Inc. All rights reserved. + * Copyright (C) 2007-2020 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 @@ -57,12 +57,19 @@ static char *UnicodeNormalizeEncodingName(const char *encoding); #define UNSUPPORTED FALSE #define IN_FULL_ICU FALSE +/* + * Thread-safe hash table to speed up encoding name -> IANA table + * index lookups. + */ +static Atomic_Ptr unicodeHashTablePtr; +static HashTable *unicodeEncCache = NULL; + static struct xRef { int MIBenum; // Assigned by IANA int winACP; // Windows code page from GetACP() StringEncoding encoding; // ICU encoding enum Bool isSupported; // VMware supported encoding - int preferredMime; // Index of preferred MIME name + int preferredMime; // Index of preferred MIME name const char *names[MAXCHARSETNAMES]; // Encoding name and aliases } xRef[] = { /* @@ -2463,13 +2470,6 @@ UnicodeNormalizeEncodingName(const char *encodingName) // IN static int UnicodeIANALookup(const char *encodingName) // IN { - /* - * Thread-safe hash table to speed up encoding name -> IANA table - * index lookups. - */ - static Atomic_Ptr htPtr; - static HashTable *encCache = NULL; - char *name = NULL; char *candidate = NULL; const char *p; @@ -2479,12 +2479,15 @@ UnicodeIANALookup(const char *encodingName) // IN void *idx; size_t windowsPrefixLen = sizeof "windows-" - 1 /* NUL */; - if (UNLIKELY(encCache == NULL)) { - encCache = HashTable_AllocOnce(&htPtr, 128, HASH_ISTRING_KEY | HASH_FLAG_ATOMIC | - HASH_FLAG_COPYKEY, free); + if (UNLIKELY(unicodeEncCache == NULL)) { + unicodeEncCache = + HashTable_AllocOnce(&unicodeHashTablePtr, 128, + HASH_ISTRING_KEY | HASH_FLAG_ATOMIC | + HASH_FLAG_COPYKEY, NULL); } - if (encCache && HashTable_Lookup(encCache, encodingName, &idx)) { + if (unicodeEncCache != NULL && + HashTable_Lookup(unicodeEncCache, encodingName, &idx)) { return (int)(uintptr_t)idx; } @@ -2546,8 +2549,8 @@ done: free(name); free(candidate); - if (encCache) { - HashTable_Insert(encCache, encodingName, (void *)(uintptr_t)i); + if (unicodeEncCache != NULL) { + HashTable_Insert(unicodeEncCache, encodingName, (void *)(uintptr_t)i); } return i; @@ -2937,6 +2940,9 @@ Unicode_Shutdown(int argc, // IN char **argv, // IN (OPT) char **envp) // IN (OPT) { + HashTable_FreeUnsafe(unicodeEncCache); + unicodeEncCache = NULL; + if (argv != NULL) { Util_FreeStringList(argv, argc + 1); } @@ -2946,6 +2952,7 @@ Unicode_Shutdown(int argc, // IN } } + #ifdef TEST_CUSTOM_ICU_DATA_FILE /* *-----------------------------------------------------------------------------