From: Wedson Almeida Filho <wedsonaf@gmail.com>
To: Rusty Russell <rusty@rustcorp.com.au>, Joe Perches <joe@perches.com>
Cc: Tim Abbott <tabbott@ksplice.com>,
linux-kernel@vger.kernel.org,
Wedson Almeida Filho <wedsonaf@gmail.com>
Subject: [PATCH v2] lib: One less subtraction in binary search iterations.
Date: Mon, 8 Jul 2013 23:37:02 -0700 [thread overview]
Message-ID: <1373351822-52050-1-git-send-email-wedsonaf@gmail.com> (raw)
In-Reply-To: <CANeycqoHsRqCU4BSkPYK5O++ciShAmFcpEx+D3XUha7kukU0bQ@mail.gmail.com>
The subtraction is removed at the expense of generality: when the element size
is 1, the array length cannot exceed half the architecture's addressable
memory.
Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com>
---
lib/bsearch.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/lib/bsearch.c b/lib/bsearch.c
index e33c179..668ae6c 100644
--- a/lib/bsearch.c
+++ b/lib/bsearch.c
@@ -24,6 +24,11 @@
* contents of the array should already be in ascending sorted order
* under the provided comparison function.
*
+ * There is a limitation when @size is 1: in this case @num must be at
+ * most SIZE_MAX / 2; that is, the number of elements in the sorted
+ * array must be at most half the maximum number expressible as a size_t
+ * to avoid overflows.
+ *
* Note that the key need not have the same type as the elements in
* the array, e.g. key could be a string and the comparison function
* could compare the string with the struct's name field. However, if
@@ -37,7 +42,7 @@ void *bsearch(const void *key, const void *base, size_t num, size_t size,
int result;
while (start < end) {
- size_t mid = start + (end - start) / 2;
+ size_t mid = (start + end) / 2;
result = cmp(key, base + mid * size);
if (result < 0)
--
1.7.9.5
next prev parent reply other threads:[~2013-07-09 6:46 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-07-06 23:07 [PATCH] lib: One less subtraction in binary search iterations Wedson Almeida Filho
2013-07-07 4:59 ` Joe Perches
2013-07-09 3:51 ` Wedson Almeida Filho
2013-07-09 4:12 ` Joe Perches
2013-07-09 4:58 ` Wedson Almeida Filho
2013-07-09 6:37 ` Wedson Almeida Filho [this message]
2013-07-15 5:07 ` [PATCH v2] " Rusty Russell
2013-07-09 7:47 ` [PATCH] " Vineet Gupta
2013-07-09 9:19 ` Mikael Pettersson
2013-07-08 1:46 ` Rusty Russell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1373351822-52050-1-git-send-email-wedsonaf@gmail.com \
--to=wedsonaf@gmail.com \
--cc=joe@perches.com \
--cc=linux-kernel@vger.kernel.org \
--cc=rusty@rustcorp.com.au \
--cc=tabbott@ksplice.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox