]> granicus.if.org Git - python/commitdiff
Issue #14630: Fix an incorrect access of ob_digit[0] for a zero instance of an int...
authorMark Dickinson <mdickinson@enthought.com>
Fri, 20 Apr 2012 20:42:49 +0000 (21:42 +0100)
committerMark Dickinson <mdickinson@enthought.com>
Fri, 20 Apr 2012 20:42:49 +0000 (21:42 +0100)
Lib/test/test_long.py
Misc/NEWS
Objects/longobject.c

index 04066ec1e79a5f28fea6bb55225bcf1c37a44254..3ebc1f7fed012a3dee7c4989a6ee7575dc804a7e 100644 (file)
@@ -1148,6 +1148,20 @@ class LongTest(unittest.TestCase):
         self.assertRaises(TypeError, myint.from_bytes, 0, 'big')
         self.assertRaises(TypeError, int.from_bytes, 0, 'big', True)
 
+    def test_access_to_nonexistent_digit_0(self):
+        # http://bugs.python.org/issue14630: A bug in _PyLong_Copy meant that
+        # ob_digit[0] was being incorrectly accessed for instances of a
+        # subclass of int, with value 0.
+        class Integer(int):
+            def __new__(cls, value=0):
+                self = int.__new__(cls, value)
+                self.foo = 'foo'
+                return self
+
+        integers = [Integer(0) for i in range(1000)]
+        for n in map(int, integers):
+            self.assertEqual(n, 0)
+
 
 def test_main():
     support.run_unittest(LongTest)
index addae1b54c6523b3cb7183189f7ec8edd328258b..56d48764b2f96f905f71df60f12aa4a41d082126 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.2.4
 Core and Builtins
 -----------------
 
+- Issue #14630: Fix a memory access bug for instances of a subclass of int
+  with value 0.
+
 - Issue #14612: Fix jumping around with blocks by setting f_lineno.
 
 - Issue #14607: Fix keyword-only arguments which started with ``__``.
index a0b16a66f2d9da50c8f3554849b0a1e0be5cf9e5..a735e33e0463bf2aa601bd71d29ae6b5212b9dad 100644 (file)
@@ -156,9 +156,7 @@ _PyLong_Copy(PyLongObject *src)
     if (i < 0)
         i = -(i);
     if (i < 2) {
-        sdigit ival = src->ob_digit[0];
-        if (Py_SIZE(src) < 0)
-            ival = -ival;
+        sdigit ival = MEDIUM_VALUE(src);
         CHECK_SMALL_INT(ival);
     }
     result = _PyLong_New(i);