boostorg / icl

Boost.org icl module
http://boost.org/libs/icl
10 stars 44 forks source link

An interval_set<DomainT> with std::numeric_limits<DomainT>::max() causes overflows #41

Open michael-veksler opened 10 months ago

michael-veksler commented 10 months ago

Consider https://godbolt.org/z/5xKWK9G5o

namespace icl = boost::icl;

int main()
{
    icl::interval_set<uint8_t> i8;
    i8.insert(std::numeric_limits<uint8_t>::max());
    fmt::print("cardinality={}", cardinality(i8));
}  

this prints cardinality=18446744073709551361 . The issue is the cardinality() at concept/interval.hpp which uses last_next(object) which overflows to 0, in this case. With interval_set, things would be worse, since these overflows would be undefined behavior.

I suggest removing the use of last_next in any situation where the interval may be the biggest interval in the set.