garray: Rewrite binary search calculation to avoid integer overflow

If `right` and `left` are both near `G_MAXUINT`, it’s possible for the
addition to overflow, even if the eventual result would fit in a
`guint`. Avoid that by operating on the difference instead.

The difference is guaranteed to be positive due to the prior `left <=
right` check.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
This commit is contained in:
Philip Withnall 2019-07-16 11:35:18 +01:00
parent bc0fcddc18
commit ec61daf503

View File

@ -845,7 +845,7 @@ g_array_binary_search (GArray *array,
while (left <= right)
{
middle = (left + right) / 2;
middle = left + (right - left) / 2;
val = compare_func (_array->data + (_array->elt_size * middle), target);
if (val < 0)