Skip to content

Commit 32175d9

Browse files
committed
Fix disjoint_pool_free() and proxy_free()
Fix disjoint_pool_free() and proxy_free() - error out on pool mismatch, when we try to free the pointer belonging to a different pool. Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 93f5530 commit 32175d9

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

src/memory_pool.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,10 @@ umf_result_t umfPoolGetTag(umf_memory_pool_handle_t hPool, void **tag) {
208208
utils_mutex_unlock(&hPool->lock);
209209
return UMF_RESULT_SUCCESS;
210210
}
211+
212+
void *umfPoolGetPoolPriv(umf_memory_pool_handle_t hPool) {
213+
if (hPool == NULL) {
214+
return NULL;
215+
}
216+
return hPool->pool_priv;
217+
}

src/memory_pool_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ typedef struct umf_memory_pool_t {
3636
void *tag;
3737
} umf_memory_pool_t;
3838

39+
void *umfPoolGetPoolPriv(umf_memory_pool_handle_t hPool);
40+
3941
#ifdef __cplusplus
4042
}
4143
#endif

src/pool/pool_disjoint.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <umf/memory_provider.h>
1818

1919
#include "base_alloc_global.h"
20+
#include "memory_pool_internal.h"
2021
#include "pool_disjoint_internal.h"
2122
#include "provider/provider_tracking.h"
2223
#include "uthash/utlist.h"
@@ -819,6 +820,11 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
819820
return ret;
820821
}
821822

823+
if (pool != umfPoolGetPoolPriv(allocInfo.pool)) {
824+
LOG_ERR("pool mismatch");
825+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
826+
}
827+
822828
size_t size = allocInfo.baseSize;
823829
umf_memory_provider_handle_t provider = disjoint_pool->provider;
824830
ret = umfMemoryProviderFree(provider, ptr, size);

src/pool/pool_proxy.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
#include <assert.h>
1414

1515
#include "base_alloc_global.h"
16+
#include "memory_pool_internal.h"
1617
#include "provider/provider_tracking.h"
1718
#include "utils_common.h"
19+
#include "utils_log.h"
1820

1921
static __TLS umf_result_t TLS_last_allocation_error;
2022

@@ -100,6 +102,11 @@ static umf_result_t proxy_free(void *pool, void *ptr) {
100102
umf_alloc_info_t allocInfo = {NULL, 0, NULL};
101103
umf_result_t umf_result = umfMemoryTrackerGetAllocInfo(ptr, &allocInfo);
102104
if (umf_result == UMF_RESULT_SUCCESS) {
105+
if (pool != umfPoolGetPoolPriv(allocInfo.pool)) {
106+
LOG_ERR("pool mismatch");
107+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
108+
}
109+
103110
size = allocInfo.baseSize;
104111
}
105112
}

0 commit comments

Comments
 (0)