[glib: 6/8] garray: Rewrite binary search calculation to avoid integer overflow



commit ec61daf5033b958778814cad8e50e6a4504d161e
Author: Philip Withnall <withnall endlessm com>
Date:   Tue Jul 16 11:35:18 2019 +0100

    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>

 glib/garray.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
---
diff --git a/glib/garray.c b/glib/garray.c
index 6e70f859e..3f13d746f 100644
--- a/glib/garray.c
+++ b/glib/garray.c
@@ -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)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]