This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH] stdlib-bsearch: middle element calculation may overflow
- From: Sergey Senozhatsky <sergey dot senozhatsky dot work at gmail dot com>
- To: "libc-alpha at sourceware dot org" <libc-alpha at sourceware dot org>
- Cc: Sergey Senozhatsky <sergey dot senozhatsky dot work at gmail dot com>, Sergey Senozhatsky <sergey dot senozhatsky at gmail dot com>
- Date: Thu, 16 Mar 2017 14:26:15 +0900
- Subject: [PATCH] stdlib-bsearch: middle element calculation may overflow
- Authentication-results: sourceware.org; auth=none
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