Commit 02617b33 authored by Jim Fulton's avatar Jim Fulton

Passing keys or values outside the range of 32-bit ints on 64-bit

platforms led to undetected overflow errors. Now these cases cause
Overflow errors to be raised.

https://bugs.launchpad.net/zodb/+bug/143237
parent ca652dc5
...@@ -22,7 +22,14 @@ ...@@ -22,7 +22,14 @@
#define KEY_CHECK PyInt_Check #define KEY_CHECK PyInt_Check
#define COPY_KEY_TO_OBJECT(O, K) O=PyInt_FromLong(K) #define COPY_KEY_TO_OBJECT(O, K) O=PyInt_FromLong(K)
#define COPY_KEY_FROM_ARG(TARGET, ARG, STATUS) \ #define COPY_KEY_FROM_ARG(TARGET, ARG, STATUS) \
if (PyInt_Check(ARG)) TARGET=PyInt_AS_LONG(ARG); else { \ if (PyInt_Check(ARG)) { \
long vcopy = PyInt_AS_LONG(ARG); \
if ((int)vcopy != vcopy) { \
PyErr_SetObject(PyExc_OverflowError, ARG); \
(STATUS)=0; (TARGET)=0; \
} \
else TARGET = vcopy; \
} else { \
PyErr_SetString(PyExc_TypeError, "expected integer key"); \ PyErr_SetString(PyExc_TypeError, "expected integer key"); \
(STATUS)=0; (TARGET)=0; } (STATUS)=0; (TARGET)=0; }
#endif #endif
......
...@@ -19,10 +19,19 @@ ...@@ -19,10 +19,19 @@
#define VALUE_TYPE int #define VALUE_TYPE int
#define VALUE_PARSE "i" #define VALUE_PARSE "i"
#define COPY_VALUE_TO_OBJECT(O, K) O=PyInt_FromLong(K) #define COPY_VALUE_TO_OBJECT(O, K) O=PyInt_FromLong(K)
#define COPY_VALUE_FROM_ARG(TARGET, ARG, STATUS) \ #define COPY_VALUE_FROM_ARG(TARGET, ARG, STATUS) \
if (PyInt_Check(ARG)) TARGET=PyInt_AsLong(ARG); else { \ if (PyInt_Check(ARG)) { \
PyErr_SetString(PyExc_TypeError, "expected integer value"); \ long vcopy = PyInt_AS_LONG(ARG); \
if ((int)vcopy != vcopy) { \
PyErr_SetObject(PyExc_OverflowError, ARG); \
(STATUS)=0; (TARGET)=0; \
} \
else TARGET = vcopy; \
} else { \
PyErr_SetString(PyExc_TypeError, "expected integer key"); \
(STATUS)=0; (TARGET)=0; } (STATUS)=0; (TARGET)=0; }
#endif #endif
#undef VALUE_TYPE_IS_PYOBJECT #undef VALUE_TYPE_IS_PYOBJECT
......
...@@ -1805,6 +1805,28 @@ class BugFixes(TestCase): ...@@ -1805,6 +1805,28 @@ class BugFixes(TestCase):
class IIBTreeTest(BTreeTests): class IIBTreeTest(BTreeTests):
def setUp(self): def setUp(self):
self.t = IIBTree() self.t = IIBTree()
def testIIBTreeOverflow(self):
good = set()
b = self.t
def trial(i):
try:
b[i] = 0
except (OverflowError, TypeError), v:
self.assertRaises(v.__class__, b.__setitem__, 0, i)
else:
good.add(i)
b[0] = i
self.assertEqual(b[0], i)
for i in range((1<<31) - 3, (1<<31) + 3):
trial(i)
trial(-i)
del b[0]
self.assertEqual(sorted(good), sorted(b))
class IFBTreeTest(BTreeTests): class IFBTreeTest(BTreeTests):
def setUp(self): def setUp(self):
self.t = IFBTree() self.t = IFBTree()
...@@ -1934,14 +1956,9 @@ class FamilyTest(TestCase): ...@@ -1934,14 +1956,9 @@ class FamilyTest(TestCase):
self.assertRaises(TypeError, s.insert, big) self.assertRaises(TypeError, s.insert, big)
self.assertRaises(TypeError, s.insert, BTrees.family32.minint - 1) self.assertRaises(TypeError, s.insert, BTrees.family32.minint - 1)
else: # 64 bit Python else: # 64 bit Python
s.insert(BTrees.family32.maxint + 1) self.assertRaises(OverflowError, s.insert, big)
self.assert_(BTrees.family32.maxint + 1 not in list(s)) self.assertRaises(OverflowError, s.insert,
# yeah, it's len of 1 now, and rolled over to the minint... BTrees.family32.minint - 1)
# don't look...don't look...
s = IOTreeSet()
s.insert(BTrees.family32.minint - 1)
self.assert_(BTrees.family32.minint - 1 not in list(s))
# similarly, this is a len of 1, rolling over to the maxint...
self.check_pickling(BTrees.family32) self.check_pickling(BTrees.family32)
def test64(self): def test64(self):
......
...@@ -20,6 +20,12 @@ Bugs fixed ...@@ -20,6 +20,12 @@ Bugs fixed
- DemoStorages didn't close their changes databases when they were - DemoStorages didn't close their changes databases when they were
created temporarily (not passed to the constructor). created temporarily (not passed to the constructor).
- Passing keys or values outside the range of 32-bit ints on 64-bit
platforms led to undetected overflow errors. Now these cases cause
Overflow errors to be raised.
https://bugs.launchpad.net/zodb/+bug/143237
3.10.0b1 (2010-05-18) 3.10.0b1 (2010-05-18)
===================== =====================
......
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