Handle UnicodeEncodeError in validate_integer

validate_integer can be potentially passed user
supplied data which causes a UnicodeEncodeError
exception during validation rather than just ValueError.
This change handles the UnicodeEncode exception
so validate_integer treats it as invalid input.

Change-Id: I5177789299b02448ed53e1b7fc23ccdaee1b70d3
Partial-Bug: 1243037
This commit is contained in:
Chris Yeoh
2013-11-20 23:24:57 +10:30
parent 84bddb030c
commit 2113dfd2e4
2 changed files with 5 additions and 1 deletions

View File

@@ -738,6 +738,10 @@ class ValidateIntegerTestCase(test.NoDBTestCase):
utils.validate_integer,
55, "doing 55 in a 54",
max_value=54)
self.assertRaises(exception.InvalidInput,
utils.validate_integer,
unichr(129), "UnicodeError",
max_value=1000)
class ValidateNeutronConfiguration(test.NoDBTestCase):

View File

@@ -968,7 +968,7 @@ def validate_integer(value, name, min_value=None, max_value=None):
"""Make sure that value is a valid integer, potentially within range."""
try:
value = int(str(value))
except ValueError:
except (ValueError, UnicodeEncodeError):
msg = _('%(value_name)s must be an integer')
raise exception.InvalidInput(reason=(
msg % {'value_name': name}))