diff options
| author | Fabio Bonelli <fb@fabiobonelli.it> | 2017-09-14 15:58:32 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2018-01-10 21:43:32 -0500 |
| commit | f636f0bb86a5ff3a61401f5ec7ea5d3392edd30f (patch) | |
| tree | 8de506351ee75c322d0e34e679a7be1546459d7f | |
| parent | c886f3dee33adc9f94332b4133a37960e985e273 (diff) | |
Fixed #29007 -- Fixed DecimalValidator crash on NaN, SNan, Inf, and Infinity values.
| -rw-r--r-- | django/core/validators.py | 3 | ||||
| -rw-r--r-- | tests/validators/tests.py | 8 |
2 files changed, 11 insertions, 0 deletions
diff --git a/django/core/validators.py b/django/core/validators.py index 15a8beafe2..3e39b9db93 100644 --- a/django/core/validators.py +++ b/django/core/validators.py @@ -391,6 +391,7 @@ class DecimalValidator: expected, otherwise raise ValidationError. """ messages = { + 'invalid': _('Enter a number.'), 'max_digits': ngettext_lazy( 'Ensure that there are no more than %(max)s digit in total.', 'Ensure that there are no more than %(max)s digits in total.', @@ -414,6 +415,8 @@ class DecimalValidator: def __call__(self, value): digit_tuple, exponent = value.as_tuple()[1:] + if exponent in {'F', 'n', 'N'}: + raise ValidationError(self.messages['invalid']) if exponent >= 0: # A positive exponent adds that many trailing zeros. digits = len(digit_tuple) + exponent diff --git a/tests/validators/tests.py b/tests/validators/tests.py index 220219789d..da3db594d9 100644 --- a/tests/validators/tests.py +++ b/tests/validators/tests.py @@ -271,6 +271,14 @@ TEST_DATA = [ (DecimalValidator(max_digits=5, decimal_places=2), Decimal('7304E-3'), ValidationError), (DecimalValidator(max_digits=5, decimal_places=5), Decimal('70E-5'), None), (DecimalValidator(max_digits=5, decimal_places=5), Decimal('70E-6'), ValidationError), + # 'Enter a number.' errors + *[ + (DecimalValidator(decimal_places=2, max_digits=10), Decimal(value), ValidationError) + for value in ( + 'NaN', '-NaN', '+NaN', 'sNaN', '-sNaN', '+sNaN', + 'Inf', '-Inf', '+Inf', 'Infinity', '-Infinity', '-Infinity', + ) + ], (validate_image_file_extension, ContentFile('contents', name='file.jpg'), None), (validate_image_file_extension, ContentFile('contents', name='file.png'), None), |
