public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Rusty Russell <rusty@rustcorp.com.au>
To: "André Goddard Rosa" <andre.goddard@gmail.com>
Cc: tabbott@ksplice.com, alan-jenkins@tuffmail.co.uk,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4 2/2] bsearch: prevent overflow when computing middle comparison element
Date: Thu, 12 Nov 2009 23:36:10 +1030	[thread overview]
Message-ID: <200911122336.11033.rusty@rustcorp.com.au> (raw)
In-Reply-To: <2bd27b061789ed14dd80816fd987273ae33ffbd1.1257864802.git.andre.goddard@gmail.com>

On Wed, 11 Nov 2009 01:30:25 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 applied all these, after testing.  In future would have been nice for you
to have posted a test patch so I didn't have make my own...

Thanks,
Rusty.

diff --git a/lib/bsearch.c b/lib/bsearch.c
--- a/lib/bsearch.c
+++ b/lib/bsearch.c
@@ -51,3 +51,50 @@ void *bsearch(const void *key, const voi
 	return NULL;
 }
 EXPORT_SYMBOL(bsearch);
+
+#if 1
+static int test_cmp(const void *_key, const void *_elt)
+{
+	int key = *(int *)_key, elt = *(int *)_elt;
+
+	if (key < elt)
+		return -1;
+	else if (key > elt)
+		return 1;
+	return 0;
+}
+
+static int test_bsearch(void)
+{
+	const int arr[] = { INT_MIN, 0, 1, 2, 3, 4, 5, 6, INT_MAX };
+	unsigned int start, num, i, total = 0;
+	int key;
+
+	for (start = 0; start < ARRAY_SIZE(arr); start++) {
+		for (num = 0; num < ARRAY_SIZE(arr) - start; num++) {
+			key = 7;
+			BUG_ON(bsearch(&key, &arr[start], num, sizeof(arr[0]),
+				       test_cmp));
+			total++;
+			for (i = start; i < start+num; i++) {
+				int *ret;
+				key = arr[i];
+				ret = bsearch(&key, &arr[start], num,
+					      sizeof(arr[0]), test_cmp);
+				if (!ret) {
+					printk("Could not find %i in %u-%u"
+					       "(between %i and %i)\n",
+					       key, start, start+num,
+					       arr[start], arr[start+num]);
+				}
+				BUG_ON(!ret);
+				BUG_ON(*ret != key);
+				total++;
+			}
+		}
+	}
+	printk("Tested %u bsearches\n", total);
+	return 0;
+}
+module_init(test_bsearch);
+#endif

  parent reply	other threads:[~2009-11-12 13:06 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <cover.1257864802.git.andre.goddard@gmail.com>
2009-11-10 15:00 ` [PATCH v4 1/2] bsearch: avoid unneeded decrement arithmetic André Goddard Rosa
2009-11-10 15:00 ` [PATCH v4 2/2] bsearch: prevent overflow when computing middle comparison element André Goddard Rosa
2009-11-11 15:09   ` Thiago Farina
2009-11-11 15:28     ` Thiago Farina
2009-11-12 13:06   ` Rusty Russell [this message]
2009-11-12 13:23     ` André Goddard Rosa
2009-11-13 15:03     ` Thiago Farina
2009-11-13 23:42       ` Rusty Russell
     [not found]         ` <AANLkTinhg-ZgQRQ5KUXR-FeTa9zo+cUF+3vZykzeaHwE@mail.gmail.com>
2010-09-23  1:26           ` Rusty Russell
2010-09-24  5:50             ` matt mooney
2010-10-04 23:32               ` 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=200911122336.11033.rusty@rustcorp.com.au \
    --to=rusty@rustcorp.com.au \
    --cc=alan-jenkins@tuffmail.co.uk \
    --cc=andre.goddard@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --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