Skip to content

Commit

Permalink
Fixed: integer overflow on 32-bit machines wasn't detected correctly
Browse files Browse the repository at this point in the history
  under Python 3.
  • Loading branch information
Jim Fulton committed May 19, 2014
1 parent 4c6f43b commit be19c1f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
7 changes: 4 additions & 3 deletions BTrees/intkeymacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
#define KEY_CHECK INT_CHECK
#define COPY_KEY_TO_OBJECT(O, K) O=INT_FROM_LONG(K)
#define COPY_KEY_FROM_ARG(TARGET, ARG, STATUS) \
if (INT_CHECK(ARG)) { \
long vcopy = INT_AS_LONG(ARG); \
if ((int)vcopy != vcopy) { \
if (INT_CHECK(ARG)) { \
long vcopy = INT_AS_LONG(ARG); \
if (PyErr_Occurred()) { (STATUS)=0; (TARGET)=0; } \
else if ((int)vcopy != vcopy) { \
PyErr_SetString(PyExc_TypeError, "integer out of range"); \
(STATUS)=0; (TARGET)=0; \
} \
Expand Down
3 changes: 2 additions & 1 deletion BTrees/intvaluemacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
#define COPY_VALUE_FROM_ARG(TARGET, ARG, STATUS) \
if (INT_CHECK(ARG)) { \
long vcopy = INT_AS_LONG(ARG); \
if ((int)vcopy != vcopy) { \
if (PyErr_Occurred()) { (STATUS)=0; (TARGET)=0; } \
else if ((int)vcopy != vcopy) { \
PyErr_SetString(PyExc_TypeError, "integer out of range"); \
(STATUS)=0; (TARGET)=0; \
} \
Expand Down
11 changes: 9 additions & 2 deletions BTrees/tests/testBTrees.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
# FOR A PARTICULAR PURPOSE
#
##############################################################################
import sys
import unittest

python3 = sys.version_info >= (3, )

from BTrees.tests.common import permutations


Expand Down Expand Up @@ -451,8 +454,12 @@ def test32(self):
# the characteristics change to match the 64 bit version, please
# feel free to change.
big = BTrees.family32.maxint + 1
self.assertRaises(TypeError, s.insert, big)
self.assertRaises(TypeError, s.insert, BTrees.family32.minint - 1)
if python3:
expected_exception = OverflowError
else:
expected_exception = TypeError
self.assertRaises(expected_exception, s.insert,
BTrees.family32.minint - 1)
self.check_pickling(BTrees.family32)

def test64(self):
Expand Down
2 changes: 2 additions & 0 deletions BTrees/tests/test_IIBTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ def trial(i):
i = int(i)
try:
b[i] = 0
except OverflowError:
self.assertRaises(OverflowError, b.__setitem__, 0, i)
except TypeError:
self.assertRaises(TypeError, b.__setitem__, 0, i)
else:
Expand Down
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
``BTrees`` Changelog
====================

- Fixed: integer overflow on 32-bit machines wasn't detected correctly
under Python 3.

4.0.9 (unreleased)
------------------
Expand Down

0 comments on commit be19c1f

Please sign in to comment.