Commit 90a7d5e2 authored by Tim Peters's avatar Tim Peters

Bucket_rangeSearch(): If the min key passed in was larger than the max key

passed in, it was quite possible for this to return *low > *high, and the
caller could crash due to trying to create a list with "negative length".
Changed the routine to consider a range empty if min>max on input, and added
test cases that fail before this patch.
parent 1c67049a
......@@ -12,7 +12,7 @@
****************************************************************************/
#define BUCKETTEMPLATE_C "$Id: BucketTemplate.c,v 1.40 2002/06/12 20:19:58 tim_one Exp $\n"
#define BUCKETTEMPLATE_C "$Id: BucketTemplate.c,v 1.41 2002/06/13 19:19:41 tim_one Exp $\n"
/* Use BUCKET_SEARCH to find the index at which a key belongs.
* INDEX An int lvalue to hold the index i such that KEY belongs at
......@@ -624,13 +624,15 @@ Bucket_rangeSearch(Bucket *self, PyObject *args, int *low, int *high)
goto empty;
}
}
else *high=self->len - 1;
else *high = self->len - 1;
return 0;
/* If f < l to begin with, it's quite possible that low > high now. */
if (*low <= *high)
return 0;
empty:
*low=0;
*high=-1;
*low = 0;
*high = -1;
return 0;
}
......
......@@ -243,12 +243,13 @@ class MappingBase(Base):
self.assertEqual(list(self.t.items()) , items)
def testEmptyRangeSearches(self):
t=self.t
t.update([(1,1),(5,5),(9,9)])
self.assertEqual(list(t.keys(-6,-4)),[], list(t.keys(-6,-4)))
self.assertEqual(list(t.keys(2,4)),[], list(t.keys(2,4)))
self.assertEqual(list(t.keys(6,8)),[], list(t.keys(6,8)))
self.assertEqual(list(t.keys(10,12)),[], list(t.keys(10,12)))
t = self.t
t.update([(1,1), (5,5), (9,9)])
self.assertEqual(list(t.keys(-6,-4)), [], list(t.keys(-6,-4)))
self.assertEqual(list(t.keys(2,4)), [], list(t.keys(2,4)))
self.assertEqual(list(t.keys(6,8)), [], list(t.keys(6,8)))
self.assertEqual(list(t.keys(10,12)), [], list(t.keys(10,12)))
self.assertEqual(list(t.keys(9, 1)), [], list(t.keys(9, 1)))
def testSlicing(self):
# Test that slicing of .keys()/.values()/.items() works exactly the
......@@ -421,12 +422,13 @@ class NormalSetTests(Base):
self.assertEqual(list(self.t.keys()) , items)
def testEmptyRangeSearches(self):
t=self.t
t.update([1,5,9])
self.assertEqual(list(t.keys(-6,-4)),[], list(t.keys(-6,-4)))
self.assertEqual(list(t.keys(2,4)),[], list(t.keys(2,4)))
self.assertEqual(list(t.keys(6,8)),[], list(t.keys(6,8)))
self.assertEqual(list(t.keys(10,12)),[], list(t.keys(10,12)))
t = self.t
t.update([1, 5, 9])
self.assertEqual(list(t.keys(-6,-4)), [], list(t.keys(-6,-4)))
self.assertEqual(list(t.keys(2,4)), [], list(t.keys(2,4)))
self.assertEqual(list(t.keys(6,8)), [], list(t.keys(6,8)))
self.assertEqual(list(t.keys(10,12)), [], list(t.keys(10,12)))
self.assertEqual(list(t.keys(9,1)), [], list(t.keys(9,1)))
def testSlicing(self):
# Test that slicing of .keys() works exactly the same way as slicing
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment