[Python-checkins] cpython: Issue #27870: A left shift of zero by a large integer no longer attempts to
mark.dickinson
python-checkins at python.org
Mon Aug 29 14:27:54 EDT 2016
https://hg.python.org/cpython/rev/09fa42818cf8
changeset: 102941:09fa42818cf8
parent: 102939:1902e1d79e25
user: Mark Dickinson <dickinsm at gmail.com>
date: Mon Aug 29 19:27:06 2016 +0100
summary:
Issue #27870: A left shift of zero by a large integer no longer attempts to allocate large amounts of memory.
files:
Lib/test/test_long.py | 15 +++++++++++++++
Misc/NEWS | 3 +++
Objects/longobject.c | 5 +++++
3 files changed, 23 insertions(+), 0 deletions(-)
diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py
--- a/Lib/test/test_long.py
+++ b/Lib/test/test_long.py
@@ -878,6 +878,21 @@
self.check_truediv(-x, y)
self.check_truediv(-x, -y)
+ def test_lshift_of_zero(self):
+ self.assertEqual(0 << 0, 0)
+ self.assertEqual(0 << 10, 0)
+ with self.assertRaises(ValueError):
+ 0 << -1
+
+ @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(0 << sys.maxsize, 0)
+ with self.assertRaises(OverflowError):
+ 0 << (sys.maxsize + 1)
+
def test_small_ints(self):
for i in range(-5, 257):
self.assertIs(i, i + 0)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
Core and Builtins
-----------------
+- Issue #27870: A left shift of zero by a large integer no longer attempts
+ to allocate large amounts of memory.
+
- Issue #25402: In int-to-decimal-string conversion, improve the estimate
of the intermediate memory required, and remove an unnecessarily strict
overflow check. Patch by Serhiy Storchaka.
diff --git a/Objects/longobject.c b/Objects/longobject.c
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -4281,6 +4281,11 @@
PyErr_SetString(PyExc_ValueError, "negative shift count");
return NULL;
}
+
+ if (Py_SIZE(a) == 0) {
+ return PyLong_FromLong(0);
+ }
+
/* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */
wordshift = shiftby / PyLong_SHIFT;
remshift = shiftby - wordshift * PyLong_SHIFT;
--
Repository URL: https://hg.python.org/cpython
More information about the Python-checkins
mailing list