From: "André Goddard Rosa" <andre.goddard@gmail.com>
To: Rusty Russell <rusty@rustcorp.com.au>
Cc: tabbott@ksplice.com, alan-jenkins@tuffmail.co.uk,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 2/2] bsearch: prevent overflow when computing middle comparison element
Date: Wed, 11 Nov 2009 13:00:39 -0200 [thread overview]
Message-ID: <b8bf37780911110700o4355aab1hd3ec3152fe2eb8c6@mail.gmail.com> (raw)
In-Reply-To: <200911111048.46705.rusty@rustcorp.com.au>
On Tue, Nov 10, 2009 at 10:18 PM, Rusty Russell <rusty@rustcorp.com.au> wrote:
> On Tue, 10 Nov 2009 02:12:31 am André Goddard Rosa wrote:
>> It's really difficult to occur in practice because the sum of the lower
>> and higher limits must overflow an int variable, but it can occur when
>> working with large arrays. We'd better safe than sorry by avoiding this
>> overflow situation when computing the middle element for comparison.
>
> I always thought the obvious answer was:
>
> mid = start + (end - start)/2;
Hi, Rusty!
Yes, you're right! The previous patch fixes the case where the
number of elements approach the
maximum int value (2^31 - 1 on my computer). If the number of elements
(parameter num) were an
integer amount (Java's array length case), just making those unsigned
would be enough, because
in the worst case we would have:
(max int) * 2 < (max unsigned int)
(2^31 - 1) * 2 < (2^32 - 1)
But it does not fix the case where the number of elements
approaches the maximum unsigned int
value (parameter size_t num).
So, the worst case happens when the number we search for is stored
at the highest extreme of the array.
In that case, 'start' tends toward 'end', and if 'end' is near the
maximum allowed value for a specific data type,
the overflow could still happen.
I'm sending a fixed patch in a moment as per your suggestion.
Thank you,
André
prev parent reply other threads:[~2009-11-11 15:00 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-11-09 14:53 [PATCH v2 0/2] bsearch: fix overflow and avoid unnecessary calculation André Goddard Rosa
2009-11-09 14:53 ` [PATCH v2 1/2] bsearch: avoid unneeded decrement arithmetic André Goddard Rosa
2009-11-09 14:53 ` [PATCH v2 2/2] bsearch: prevent overflow when computing middle comparison element André Goddard Rosa
2009-11-11 0:18 ` [PATCH v3 " Rusty Russell
2009-11-11 15:00 ` André Goddard Rosa [this message]
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=b8bf37780911110700o4355aab1hd3ec3152fe2eb8c6@mail.gmail.com \
--to=andre.goddard@gmail.com \
--cc=alan-jenkins@tuffmail.co.uk \
--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
all inboxes | Powered by JetHome®