fix sort overflows
rurban opened this issue · comments
Reini Urban commented
two major bugs detected by formal verification.
first the well-known binary search mid bug: (a + b) / 2
needs to replaced with Dietz formula.
http://aggregate.org/MAGIC/#Average%20of%20Integers
which is also much faster than a division.
this was previously hacked by restricting a and b to signed long.
second:
z and z+1 could also overflow. this an be simply checked with if (z+1 > z)
sort accepts now the full unsigned long, ie. size_t range.
Reini Urban commented