Skip to content

Commit

Permalink
Fixed #25767 -- Fixed data truncation possibility with Positive(Small…
Browse files Browse the repository at this point in the history
…)IntegerField on MySQL.
  • Loading branch information
georgemarshall authored and timgraham committed Nov 24, 2015
1 parent a918f8b commit 710e11d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
4 changes: 2 additions & 2 deletions django/db/backends/mysql/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class DatabaseOperations(BaseDatabaseOperations):

# MySQL stores positive fields as UNSIGNED ints.
integer_field_ranges = dict(BaseDatabaseOperations.integer_field_ranges,
PositiveSmallIntegerField=(0, 4294967295),
PositiveIntegerField=(0, 18446744073709551615),
PositiveSmallIntegerField=(0, 65535),
PositiveIntegerField=(0, 4294967295),
)

def date_extract_sql(self, lookup_type, field_name):
Expand Down
5 changes: 5 additions & 0 deletions docs/releases/1.8.7.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ Bugfixes
* Fixed ``set_FOO_order()`` crash when the ``ForeignKey`` of a model with
``order_with_respect_to`` references a model with a ``OneToOneField``
primary key (:ticket:`25786`).

* Fixed incorrect validation for ``PositiveIntegerField`` and
``PositiveSmallIntegerField`` on MySQL resulting in values greater than
4294967295 or 65535, respectively, passing validation and being silently
truncated by the database (:ticket:`25767`).
32 changes: 29 additions & 3 deletions tests/model_fields/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,12 @@ class IntegerFieldTests(test.TestCase):
model = IntegerModel
documented_range = (-2147483648, 2147483647)

@property
def backend_range(self):
field = self.model._meta.get_field('value')
internal_type = field.get_internal_type()
return connection.ops.integer_field_range(internal_type)

def test_documented_range(self):
"""
Ensure that values within the documented safe range pass validation,
Expand All @@ -645,14 +651,34 @@ def test_documented_range(self):
self.assertEqual(qs.count(), 1)
self.assertEqual(qs[0].value, max_value)

def test_backend_range_save(self):
"""
Ensure that backend specific range can be saved without corruption.
"""
min_value, max_value = self.backend_range

if min_value is not None:
instance = self.model(value=min_value)
instance.full_clean()
instance.save()
qs = self.model.objects.filter(value__lte=min_value)
self.assertEqual(qs.count(), 1)
self.assertEqual(qs[0].value, min_value)

if max_value is not None:
instance = self.model(value=max_value)
instance.full_clean()
instance.save()
qs = self.model.objects.filter(value__gte=max_value)
self.assertEqual(qs.count(), 1)
self.assertEqual(qs[0].value, max_value)

def test_backend_range_validation(self):
"""
Ensure that backend specific range are enforced at the model
validation level. ref #12030.
"""
field = self.model._meta.get_field('value')
internal_type = field.get_internal_type()
min_value, max_value = connection.ops.integer_field_range(internal_type)
min_value, max_value = self.backend_range

if min_value is not None:
instance = self.model(value=min_value - 1)
Expand Down

0 comments on commit 710e11d

Please sign in to comment.