Repository navigation
Fix DecimalField crash on long numbers when max_digits is None - #10123
mohamed-alired wants to merge 1 commit into
Conversation
quantize() only set the decimal context precision when max_digits was given, so with max_digits=None it kept the default precision of 28 and Decimal.quantize() raised InvalidOperation for any value needing more digits (e.g. '1' * 27 with decimal_places=2, or '1e30'): a 500 instead of a valid value. Give the context enough precision for the quantized value, bounded by MAX_STRING_LENGTH like the input itself, and report values beyond that bound (e.g. '1e1000') as a validation error.
auvipy
left a comment
There was a problem hiding this comment.
which issue you are trying to fix?
ege-arhan
left a comment
There was a problem hiding this comment.
to_internal_value() now turns the oversized value into a validation error, but to_representation() still raises for the same values, so the "to_representation() has the same problem" part of the description isn't covered by the diff.
from decimal import Decimal
from types import SimpleNamespace
from rest_framework import serializers
class S(serializers.Serializer):
amount = serializers.DecimalField(max_digits=None, decimal_places=2)
S(instance=SimpleNamespace(amount=Decimal("1e999"))).data
# decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]Worst case with this branch: any value with value.adjusted() >= 998 reaches quantize() (fields.py:1150-1151), where digits is clamped to MAX_STRING_LENGTH and the quantize no longer has room. A Decimal that big can come out of Postgres numeric (max around 1e131072), so it gets here from a model instance without passing through this serializer's input at all — that was the 500 in the description.
On the input side, S(data={"amount": "1e999"}).is_valid() now reports String value too large. for a 5-character string, which sends people looking at the wrong limit (invalid, or a message that mentions the digit bound, would read better).
I'd catch InvalidOperation in to_representation() the same way, or drop the MAX_STRING_LENGTH clamp in quantize() and use the digits the quantized result needs. The clamp only makes sense for the input string, and the string is already length-checked at fields.py:1055.
Checked with pytest tests/test_fields.py -k Decimal on 2be5057: 39 passed.
Description
DecimalField.quantize()only sets the decimal context precision whenmax_digitsis given. Withmax_digits=None(documented as allowed, and covered byTestNoMaxDigitsDecimalField) it keeps the default context precision of 28, soDecimal.quantize()raisesdecimal.InvalidOperationfor any value that needs more digits once quantized. The exception is not caught, so the request fails with a 500:to_representation()has the same problem for such values.This change:
max_digitsisNone, bounded byMAX_STRING_LENGTHlike the input string itself, so a short input with a huge exponent (e.g.'1e999999999') cannot make the server build an enormous number;max_string_length) instead of a 500.Behaviour with
max_digitsset is unchanged.Tests:
TestNoMaxDigitsLargeDecimalFieldcovers 27-digit and1e30inputs, an integer input, an oversized exponent and a 30-digit output. It fails onmainwithInvalidOperationand passes with this change.