[PATCH] stdlib-bsearch: middle element calculation may overflow

Sergey Senozhatsky sergey.senozhatsky.work@gmail.com
Thu Mar 16 05:26:00 GMT 2017


Middle element calculation may overflow at '__l + __u' when
__l and __u are large enough. Use distance between __u and
__l instead.

Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
---
 bits/stdlib-bsearch.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bits/stdlib-bsearch.h b/bits/stdlib-bsearch.h
index eb145381fd..5fd8a8b607 100644
--- a/bits/stdlib-bsearch.h
+++ b/bits/stdlib-bsearch.h
@@ -28,7 +28,7 @@ bsearch (const void *__key, const void *__base, size_t __nmemb, size_t __size,
   __u = __nmemb;
   while (__l < __u)
     {
-      __idx = (__l + __u) / 2;
+      __idx = __l + (__u - __l) / 2;
       __p = (void *) (((const char *) __base) + (__idx * __size));
       __comparison = (*__compar) (__key, __p);
       if (__comparison < 0)
-- 
2.12.0



More information about the Libc-alpha mailing list