summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_int.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-11-20 19:56:21 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-11-20 19:56:21 (GMT)
commit6156560e4b40ae81304d80b5a932fd90e6b4ba80 (patch)
treed8ed8e08a60b0c9ed7f3cb582d5765fa1f814643 /Lib/test/test_int.py
parent815ab140302a2f7a541d1bbda650875bd47f8ea2 (diff)
downloadcpython-6156560e4b40ae81304d80b5a932fd90e6b4ba80.zip
cpython-6156560e4b40ae81304d80b5a932fd90e6b4ba80.tar.gz
cpython-6156560e4b40ae81304d80b5a932fd90e6b4ba80.tar.bz2
Issue #25678: Copy buffer objects to null-terminated strings.
Avoid buffer overreads when int(), long(), float(), and compile() are passed buffer objects. Similar code is removed from the complex() constructor, where it was not reachable. Patch backported from issue #24802 by Eryk Sun.
Diffstat (limited to 'Lib/test/test_int.py')
-rw-r--r--Lib/test/test_int.py25
1 files changed, 21 insertions, 4 deletions
diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py
index 365f9a2..2ca6cf2 100644
--- a/Lib/test/test_int.py
+++ b/Lib/test/test_int.py
@@ -340,20 +340,37 @@ class IntTestCases(IntLongCommonTests, unittest.TestCase):
# Test possible valid non-numeric types for x, including subclasses
# of the allowed built-in types.
class CustomStr(str): pass
- values = ['100', CustomStr('100')]
+ class CustomByteArray(bytearray): pass
+ factories = [str, bytearray, CustomStr, CustomByteArray, buffer]
if have_unicode:
class CustomUnicode(unicode): pass
- values += [unicode('100'), CustomUnicode(unicode('100'))]
+ factories += [unicode, CustomUnicode]
- for x in values:
+ for f in factories:
+ x = f('100')
msg = 'x has value %s and type %s' % (x, type(x).__name__)
try:
self.assertEqual(int(x), 100, msg=msg)
- self.assertEqual(int(x, 2), 4, msg=msg)
+ if isinstance(x, basestring):
+ self.assertEqual(int(x, 2), 4, msg=msg)
except TypeError, err:
raise AssertionError('For %s got TypeError: %s' %
(type(x).__name__, err))
+ if not isinstance(x, basestring):
+ errmsg = "can't convert non-string"
+ with self.assertRaisesRegexp(TypeError, errmsg, msg=msg):
+ int(x, 2)
+ errmsg = 'invalid literal'
+ with self.assertRaisesRegexp(ValueError, errmsg, msg=msg):
+ int(f('A' * 0x10))
+
+ def test_int_buffer(self):
+ self.assertEqual(int(buffer('123', 1, 2)), 23)
+ self.assertEqual(int(buffer('123\x00', 1, 2)), 23)
+ self.assertEqual(int(buffer('123 ', 1, 2)), 23)
+ self.assertEqual(int(buffer('123A', 1, 2)), 23)
+ self.assertEqual(int(buffer('1234', 1, 2)), 23)
def test_error_on_string_float_for_x(self):
self.assertRaises(ValueError, int, '1.2')