mirror of
https://github.com/python/cpython.git
synced 2025-08-30 21:48:47 +00:00
Issue #19638: Raise ValueError instead of crashing when converting billion character strings to float.
This commit is contained in:
parent
22dc4d5fa8
commit
f45bbb6211
3 changed files with 77 additions and 12 deletions
|
@ -248,6 +248,37 @@ class StrtodTests(unittest.TestCase):
|
|||
else:
|
||||
assert False, "expected ValueError"
|
||||
|
||||
@test.support.bigmemtest(size=5 * test.support._1G, memuse=1, dry_run=False)
|
||||
def test_oversized_digit_strings(self, maxsize):
|
||||
# Input string whose length doesn't fit in an INT.
|
||||
s = "1." + "1" * int(2.2e9)
|
||||
with self.assertRaises(ValueError):
|
||||
float(s)
|
||||
del s
|
||||
|
||||
s = "0." + "0" * int(2.2e9) + "1"
|
||||
with self.assertRaises(ValueError):
|
||||
float(s)
|
||||
del s
|
||||
|
||||
def test_large_exponents(self):
|
||||
# Verify that the clipping of the exponent in strtod doesn't affect the
|
||||
# output values.
|
||||
def positive_exp(n):
|
||||
""" Long string with value 1.0 and exponent n"""
|
||||
return '0.{}1e+{}'.format('0'*(n-1), n)
|
||||
|
||||
def negative_exp(n):
|
||||
""" Long string with value 1.0 and exponent -n"""
|
||||
return '1{}e-{}'.format('0'*n, n)
|
||||
|
||||
self.assertEqual(float(positive_exp(10000)), 1.0)
|
||||
self.assertEqual(float(positive_exp(20000)), 1.0)
|
||||
self.assertEqual(float(positive_exp(30000)), 1.0)
|
||||
self.assertEqual(float(negative_exp(10000)), 1.0)
|
||||
self.assertEqual(float(negative_exp(20000)), 1.0)
|
||||
self.assertEqual(float(negative_exp(30000)), 1.0)
|
||||
|
||||
def test_particular(self):
|
||||
# inputs that produced crashes or incorrectly rounded results with
|
||||
# previous versions of dtoa.c, for various reasons
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue