]> granicus.if.org Git - python/commitdiff
Issue #27870: A left shift of zero by a large integer no longer attempts to allocate...
authorMark Dickinson <dickinsm@gmail.com>
Mon, 29 Aug 2016 18:38:12 +0000 (19:38 +0100)
committerMark Dickinson <dickinsm@gmail.com>
Mon, 29 Aug 2016 18:38:12 +0000 (19:38 +0100)
Lib/test/test_long.py
Misc/NEWS
Objects/longobject.c

index b65d24cd9418913a8f496a10cc1c34b284f9c413..6788cedd809decb74e3b96199056e1b2de176034 100644 (file)
@@ -202,6 +202,21 @@ class LongTest(test_int.IntLongCommonTests, unittest.TestCase):
                 self.assertEqual(x, y,
                     Frm("bad result for a*b: a=%r, b=%r, x=%r, y=%r", a, b, x, y))
 
+    def test_lshift_of_zero(self):
+        self.assertEqual(0L << 0, 0)
+        self.assertEqual(0L << 10, 0)
+        with self.assertRaises(ValueError):
+            0L << -1
+
+    @test_support.cpython_only
+    def test_huge_lshift_of_zero(self):
+        # Shouldn't try to allocate memory for a huge shift. See issue #27870.
+        # Other implementations may have a different boundary for overflow,
+        # or not raise at all.
+        self.assertEqual(0L << sys.maxsize, 0)
+        with self.assertRaises(OverflowError):
+            0L << (sys.maxsize + 1)
+
     def check_bitop_identities_1(self, x):
         eq = self.assertEqual
         eq(x & 0, 0, Frm("x & 0 != 0 for x=%r", x))
index 1fd3d85e89cffc16f7b8fd907153bc7ac36d5cd4..2701c7b88b1397a60cf1d4d965c4e2e527945a62 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 2.7.13?
 Core and Builtins
 -----------------
 
+- Issue #27870: A left shift of zero by a large integer no longer attempts
+  to allocate large amounts of memory.
+
 - Issue #25604: Fix a minor bug in integer true division; this bug could
   potentially have caused off-by-one-ulp results on platforms with
   unreliable ldexp implementations.
index 6f469bf3e32e4f88ef0d2ebfffd7dfb30a8d6d43..de2b1399bf0152cfad9ba3713401391a186ac52e 100644 (file)
@@ -3715,6 +3715,11 @@ long_lshift(PyObject *v, PyObject *w)
         PyErr_SetString(PyExc_ValueError, "negative shift count");
         goto lshift_error;
     }
+
+    if (Py_SIZE(a) == 0) {
+        return PyLong_FromLong(0);
+    }
+
     /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */
     wordshift = shiftby / PyLong_SHIFT;
     remshift  = shiftby - wordshift * PyLong_SHIFT;