Re: [hub] Andrzej's review

Joaquin M López Muñoz via Boost <[email protected]>
Newsgroups gmane.comp.lib.boost.devel
Message-ID <[email protected]>
El 23/04/2026 a las 18:52, Peter Dimov via Boost escribió:
>> Andrzej Krzemienski wrote:
>>> 5. Compiling with -Wall gives me a warning in GCC when sorting an
>>> empty
>>> hub: a zero-sized heap array is allocated:
>>> https://godbolt.org/z/dWvxv9n8s
>>> This is technically fine to allocate such arrays, but maybe it is
>>> worth to make -Wall clean.
>> The warning isn't caused by the zero sized allocation (which is legal.)
>>
>> It's because the compiler can see that the array has zero elements, but can't
>> see that the loop inside this call to visit
>>
>> https://github.com/joaquintides/hub/blob/063cf1d3589d4893b53964084ee
>> 56f4476eed471/include/boost/container/hub.hpp#L1628
>>
>> is never executed. So it says "if this loop is executed, it would be UB because
>> there aren't any elements there" but this is a false positive because the loop
>> isn't executed.
>>
>> (The subsequent loops suffer from the same issue and also cause warnings for
>> the same reason.)
>>
>> A common problem with GCC, and unfortunately nothing can be done about it
>> short of disabling the warning.
> On second thought, given that the warning only fires on empty containers, it
> might be possible to avoid if `sort` checks for `empty()` (or `size() <= 1`) and returns.
>
> Adding a branch to avoid a warning is a bit dubious in general, but in this case
> it avoids a completely unnecessary allocation as well, so it's sound even in principle. :-)

Yes, I'll do that, the cost is negligible and the other two internal 
sorting functions (proxy_sort and compact_sort) have an analogous check 
(though in this case it's necessary).

Joaquín M López Muñoz

_______________________________________________
Boost mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://lists.boost.org/mailman3/lists/boost.lists.boost.org/
Archived at: https://lists.boost.org/archives/list/[email protected]/message/U5RTYYOIEYEBAKLQCDSV6CG7XQRLL7DI/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.