Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bcc17ee

Browse files
committed
Issue #14630: Fix an incorrect access of ob_digit[0] for a zero instance of an int subclass.
1 parent 63674f4 commit bcc17ee

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

Lib/test/test_long.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,20 @@ class myint(int):
11481148
self.assertRaises(TypeError, myint.from_bytes, 0, 'big')
11491149
self.assertRaises(TypeError, int.from_bytes, 0, 'big', True)
11501150

1151+
def test_access_to_nonexistent_digit_0(self):
1152+
# http://bugs.python.org/issue14630: A bug in _PyLong_Copy meant that
1153+
# ob_digit[0] was being incorrectly accessed for instances of a
1154+
# subclass of int, with value 0.
1155+
class Integer(int):
1156+
def __new__(cls, value=0):
1157+
self = int.__new__(cls, value)
1158+
self.foo = 'foo'
1159+
return self
1160+
1161+
integers = [Integer(0) for i in range(1000)]
1162+
for n in map(int, integers):
1163+
self.assertEqual(n, 0)
1164+
11511165

11521166
def test_main():
11531167
support.run_unittest(LongTest)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.2.4
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #14630: Fix a memory access bug for instances of a subclass of int
14+
with value 0.
15+
1316
- Issue #14612: Fix jumping around with blocks by setting f_lineno.
1417

1518
- Issue #14607: Fix keyword-only arguments which started with ``__``.

Objects/longobject.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,7 @@ _PyLong_Copy(PyLongObject *src)
156156
if (i < 0)
157157
i = -(i);
158158
if (i < 2) {
159-
sdigit ival = src->ob_digit[0];
160-
if (Py_SIZE(src) < 0)
161-
ival = -ival;
159+
sdigit ival = MEDIUM_VALUE(src);
162160
CHECK_SMALL_INT(ival);
163161
}
164162
result = _PyLong_New(i);

0 commit comments

Comments
 (0)