microsoft / mimalloc

mimalloc is a compact general purpose allocator with excellent performance.
MIT License
10.63k stars 867 forks source link

Fix for GCC warning #919

Closed CunningBaldrick closed 3 months ago

CunningBaldrick commented 3 months ago

In spite of commit af3f2f9168fc0f2345c23d8c8b34a73563935834 (fix assertions and warnings on gcc), I get this warning

.../src/segment.c: In function 'mi_segment_span_free_coalesce': .../src/segment.c:665:30: warning: '__atomic_load_8' writing 8 bytes into a region of size 0 overflows the destination [-Wstringop-overflow=] 665 | const bool is_abandoned = (segment->thread_id == 0); // mi_segment_is_abandoned(segment); | ^~~

when compiling with GCC 12.3.1. The reason: the compiler thinks that segment might be NULL (_mi_ptr_segment explicitly returns NULL in some cases). So I suggest this patch, which moves the call to mi_segment_is_abandoned a few lines later, after segment has been dereferenced (so the compiler knows that segment can't be NULL):

--- a/src/segment.c +++ b/src/segment.c @@ -662,7 +662,6 @@ static void mi_segment_span_remove_from_queue(mi_slice_t slice, mi_segments_tld static mi_slice_t mi_segment_span_free_coalesce(mi_slice_t slice, mi_segments_tld_t tld) { mi_assert_internal(slice != NULL && slice->slice_count > 0 && slice->slice_offset == 0); mi_segment_t* const segment = _mi_ptr_segment(slice);

daanx commented 3 months ago

Great -- I moved the declaration; thanks so much!