Skip to content

Commit 353d293

Browse files
committed
Do not free a pointer from another pool
Signed-off-by: Lukasz Dorau <[email protected]>
1 parent feeb424 commit 353d293

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

include/umf/base.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ typedef enum umf_result_t {
4747
6, ///< Failure in user provider code (i.e in user provided callback)
4848
UMF_RESULT_ERROR_DEPENDENCY_UNAVAILABLE =
4949
7, ///< External required dependency is unavailable or missing
50+
UMF_RESULT_ERROR_INVALID_FREE_OP =
51+
8, /*!< Invalid free operation - trying to free a pointer used by another
52+
pool or a pointer belonging to another pool */
5053
UMF_RESULT_ERROR_UNKNOWN = 0x7ffffffe ///< Unknown or internal error
5154
} umf_result_t;
5255

src/memory_pool.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ size_t umfPoolMallocUsableSize(umf_memory_pool_handle_t hPool, void *ptr) {
180180

181181
umf_result_t umfPoolFree(umf_memory_pool_handle_t hPool, void *ptr) {
182182
UMF_CHECK((hPool != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
183+
umf_memory_pool_handle_t poolTracker = umfMemoryTrackerGetPool(ptr);
184+
if (poolTracker != NULL && poolTracker != hPool) {
185+
LOG_ERR("invalid free operation: trying to free a pointer %p used by "
186+
"another pool or belonging to another pool %p than the given "
187+
"pool %p",
188+
ptr, poolTracker, hPool);
189+
return UMF_RESULT_ERROR_INVALID_FREE_OP;
190+
}
183191
return hPool->ops.free(hPool->pool_priv, ptr);
184192
}
185193

0 commit comments

Comments
 (0)