rurban / ctl

My variant of the C Template Library

Home Page:https://rurban.github.io/ctl/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

fix sort overflows

rurban opened this issue · comments

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.

Fixed with cd971c5
and improved with b7e19da