Skip to content

Commit eae8d63

Browse files
authored
Merge pull request #1148 from ldorau/coarse_error_out_on_double_free_instead_of_assert
coarse: error out on double free instead of assert
2 parents 4c70f6c + c2e758e commit eae8d63

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/coarse/coarse.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -1170,10 +1170,13 @@ umf_result_t coarse_free(coarse_t *coarse, void *ptr, size_t bytes) {
11701170
}
11711171

11721172
block_t *block = get_node_block(node);
1173-
assert(block->used);
1173+
if (!block->used) {
1174+
LOG_ERR("double free");
1175+
utils_mutex_unlock(&coarse->lock);
1176+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1177+
}
11741178

11751179
if (bytes > 0 && bytes != block->size) {
1176-
// wrong size of allocation
11771180
LOG_ERR("wrong size of allocation");
11781181
utils_mutex_unlock(&coarse->lock);
11791182
return UMF_RESULT_ERROR_INVALID_ARGUMENT;

test/coarse_lib.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ TEST_P(CoarseWithMemoryStrategyTest, coarseTest_basic_provider) {
160160
ASSERT_EQ(coarse_get_stats(ch).alloc_size, alloc_size);
161161
ASSERT_EQ(coarse_get_stats(ch).num_all_blocks, 1);
162162

163+
// test double free
164+
umf_result = coarse_free(ch, ptr, 2 * MB);
165+
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT);
166+
ASSERT_EQ(coarse_get_stats(ch).used_size, 0);
167+
ASSERT_EQ(coarse_get_stats(ch).alloc_size, alloc_size);
168+
ASSERT_EQ(coarse_get_stats(ch).num_all_blocks, 1);
169+
163170
coarse_delete(ch);
164171
umfMemoryProviderDestroy(malloc_memory_provider);
165172
}
@@ -202,6 +209,13 @@ TEST_P(CoarseWithMemoryStrategyTest, coarseTest_basic_fixed_memory) {
202209
ASSERT_EQ(coarse_get_stats(ch).alloc_size, buff_size);
203210
ASSERT_EQ(coarse_get_stats(ch).num_all_blocks, 1);
204211

212+
// test double free
213+
umf_result = coarse_free(ch, ptr, 2 * MB);
214+
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT);
215+
ASSERT_EQ(coarse_get_stats(ch).used_size, 0);
216+
ASSERT_EQ(coarse_get_stats(ch).alloc_size, buff_size);
217+
ASSERT_EQ(coarse_get_stats(ch).num_all_blocks, 1);
218+
205219
coarse_delete(ch);
206220
}
207221

0 commit comments

Comments
 (0)