summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2017-10-03 12:38:46 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2017-10-03 12:38:46 (GMT)
commitb5a630f3dd30ed628e088efe7523e650087adba2 (patch)
tree08f4a920aeeef6f08270a531513930ffc2a95cdc
parentec47aff13a3f114787f3ae99b2a7bbdd0162f510 (diff)
downloadcpython-b5a630f3dd30ed628e088efe7523e650087adba2.zip
cpython-b5a630f3dd30ed628e088efe7523e650087adba2.tar.gz
cpython-b5a630f3dd30ed628e088efe7523e650087adba2.tar.bz2
[3.6] bpo-31619: Fixed a ValueError when convert a string with large number of underscores (GH-3827) (#3863)
to integer with binary base. (cherry picked from commit 85c0b8941f0c8ef3ed787c9d504712c6ad3eb5d3)
-rw-r--r--Lib/test/test_int.py8
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2017-09-29-20-32-24.bpo-31619.6gQ1kv.rst2
-rw-r--r--Objects/longobject.c8
3 files changed, 14 insertions, 4 deletions
diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py
index 14bbd61..a36076e 100644
--- a/Lib/test/test_int.py
+++ b/Lib/test/test_int.py
@@ -506,5 +506,13 @@ class IntTestCases(unittest.TestCase):
check('123\ud800')
check('123\ud800', 10)
+ def test_issue31619(self):
+ self.assertEqual(int('1_0_1_0_1_0_1_0_1_0_1_0_1_0_1_0_1_0_1_0_1_0_1_0_1_0_1_0_1_0_1', 2),
+ 0b1010101010101010101010101010101)
+ self.assertEqual(int('1_2_3_4_5_6_7_0_1_2_3', 8), 0o12345670123)
+ self.assertEqual(int('1_2_3_4_5_6_7_8_9', 16), 0x123456789)
+ self.assertEqual(int('1_2_3_4_5_6_7', 32), 1144132807)
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-09-29-20-32-24.bpo-31619.6gQ1kv.rst b/Misc/NEWS.d/next/Core and Builtins/2017-09-29-20-32-24.bpo-31619.6gQ1kv.rst
new file mode 100644
index 0000000..3efcc9d
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2017-09-29-20-32-24.bpo-31619.6gQ1kv.rst
@@ -0,0 +1,2 @@
+Fixed a ValueError when convert a string with large number of underscores
+to integer with binary base.
diff --git a/Objects/longobject.c b/Objects/longobject.c
index ad239ce..450087b 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -2049,15 +2049,15 @@ long_from_binary_base(const char **str, int base, PyLongObject **res)
}
*str = p;
- /* n <- # of Python digits needed, = ceiling(n/PyLong_SHIFT). */
- n = digits * bits_per_char + PyLong_SHIFT - 1;
- if (n / bits_per_char < p - start) {
+ /* n <- the number of Python digits needed,
+ = ceiling((digits * bits_per_char) / PyLong_SHIFT). */
+ if (digits > (PY_SSIZE_T_MAX - (PyLong_SHIFT - 1)) / bits_per_char) {
PyErr_SetString(PyExc_ValueError,
"int string too large to convert");
*res = NULL;
return 0;
}
- n = n / PyLong_SHIFT;
+ n = (digits * bits_per_char + PyLong_SHIFT - 1) / PyLong_SHIFT;
z = _PyLong_New(n);
if (z == NULL) {
*res = NULL;