diff --git a/winpr/include/winpr/collections.h b/winpr/include/winpr/collections.h index 63307ac3e..3c34703f4 100644 --- a/winpr/include/winpr/collections.h +++ b/winpr/include/winpr/collections.h @@ -293,7 +293,7 @@ extern "C" typedef struct _wHashTable wHashTable; - typedef BOOL (*HASH_TABLE_FOREACH_FN)(const void* key, const void* value, void* arg); + typedef BOOL (*HASH_TABLE_FOREACH_FN)(const void* key, void* value, void* arg); WINPR_API size_t HashTable_Count(wHashTable* table); WINPR_API BOOL HashTable_Insert(wHashTable* table, const void* key, const void* value); diff --git a/winpr/libwinpr/utils/collections/HashTable.c b/winpr/libwinpr/utils/collections/HashTable.c index 9302648e7..7c0937b95 100644 --- a/winpr/libwinpr/utils/collections/HashTable.c +++ b/winpr/libwinpr/utils/collections/HashTable.c @@ -698,32 +698,31 @@ BOOL HashTable_ContainsValue(wHashTable* table, const void* value) wHashTable* HashTable_New(BOOL synchronized) { - wHashTable* table; - table = (wHashTable*)calloc(1, sizeof(wHashTable)); + wHashTable* table = (wHashTable*)calloc(1, sizeof(wHashTable)); - if (table) - { - table->synchronized = synchronized; - InitializeCriticalSectionAndSpinCount(&(table->lock), 4000); - table->numOfBuckets = 64; - table->numOfElements = 0; - table->bucketArray = (wKeyValuePair**)calloc(table->numOfBuckets, sizeof(wKeyValuePair*)); + if (!table) + goto fail; - if (!table->bucketArray) - { - free(table); - return NULL; - } + table->synchronized = synchronized; + InitializeCriticalSectionAndSpinCount(&(table->lock), 4000); + table->numOfBuckets = 64; + table->numOfElements = 0; + table->bucketArray = (wKeyValuePair**)calloc(table->numOfBuckets, sizeof(wKeyValuePair*)); - table->idealRatio = 3.0; - table->lowerRehashThreshold = 0.0; - table->upperRehashThreshold = 15.0; - table->hash = HashTable_PointerHash; - table->key.fnObjectEquals = HashTable_PointerCompare; - table->value.fnObjectEquals = HashTable_PointerCompare; - } + if (!table->bucketArray) + goto fail; + + table->idealRatio = 3.0; + table->lowerRehashThreshold = 0.0; + table->upperRehashThreshold = 15.0; + table->hash = HashTable_PointerHash; + table->key.fnObjectEquals = HashTable_PointerCompare; + table->value.fnObjectEquals = HashTable_PointerCompare; return table; +fail: + HashTable_Free(table); + return NULL; } void HashTable_Free(wHashTable* table) @@ -732,7 +731,10 @@ void HashTable_Free(wHashTable* table) wKeyValuePair* pair; wKeyValuePair* nextPair; - if (table) + if (!table) + return; + + if (table->bucketArray) { for (index = 0; index < table->numOfBuckets; index++) { @@ -746,11 +748,11 @@ void HashTable_Free(wHashTable* table) pair = nextPair; } } - - DeleteCriticalSection(&(table->lock)); free(table->bucketArray); - free(table); } + DeleteCriticalSection(&(table->lock)); + + free(table); } wObject* HashTable_KeyObject(wHashTable* table) diff --git a/winpr/libwinpr/utils/test/TestHashTable.c b/winpr/libwinpr/utils/test/TestHashTable.c index dd1d14b37..455736ff4 100644 --- a/winpr/libwinpr/utils/test/TestHashTable.c +++ b/winpr/libwinpr/utils/test/TestHashTable.c @@ -294,7 +294,7 @@ typedef struct BOOL test3error; } ForeachData; -BOOL foreachFn1(const void* key, const void* value, void* arg) +BOOL foreachFn1(const void* key, void* value, void* arg) { ForeachData* d = (ForeachData*)arg; WINPR_UNUSED(key); @@ -302,7 +302,7 @@ BOOL foreachFn1(const void* key, const void* value, void* arg) return TRUE; } -BOOL foreachFn2(const void* key, const void* value, void* arg) +BOOL foreachFn2(const void* key, void* value, void* arg) { ForeachData* d = (ForeachData*)arg; WINPR_UNUSED(key); @@ -314,7 +314,7 @@ BOOL foreachFn2(const void* key, const void* value, void* arg) return TRUE; } -BOOL foreachFn3(const void* key, const void* value, void* arg) +BOOL foreachFn3(const void* key, void* value, void* arg) { char* keyStr = (char*)key;