Skip to content

Commit

Permalink
add missing float/double bounds check (#128)
Browse files Browse the repository at this point in the history
* add missing float/double bounds check

Signed-off-by: Seulbae Kim <squizz617@gmail.com>
Co-authored-by: Chris Lalancette <clalancette@openrobotics.org>
  • Loading branch information
squizz617 and clalancette committed Jun 15, 2021
1 parent c6272b8 commit 7638b52
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
30 changes: 30 additions & 0 deletions rosidl_generator_py/resource/_msg.py.em
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,22 @@ bound = 2**nbits
@[ elif isinstance(type_, BasicType) and type_.typename == 'char']@
all(val >= 0 and val) < 256 for val in value)), \
@{assert_msg_suffixes.append('and each char in [0, 255]')}@
@[ elif isinstance(type_, BasicType) and type_.typename in FLOATING_POINT_TYPES]@
@[ if type_.typename == "float"]@
@{
name = "float"
bound = 3.402823e+38
}@
all(val >= -@(bound) and val <= @(bound) for val in value)), \
@{assert_msg_suffixes.append('and each float in [%f, %f]' % (-bound, bound))}@
@[ elif type_.typename == "double"]@
@{
name = "double"
bound = 1.7976931348623157e+308
}@
all(val >= -@(bound) and val <= @(bound) for val in value)), \
@{assert_msg_suffixes.append('and each double in [%f, %f]' % (-bound, bound))}@
@[ end if]@
@[ else]@
True), \
@[ end if]@
Expand Down Expand Up @@ -538,6 +554,20 @@ bound = 2**nbits
}@
assert value >= 0 and value < @(bound), \
"The '@(member.name)' field must be an unsigned integer in [0, @(bound - 1)]"
@[ elif type_.typename in FLOATING_POINT_TYPES]@
@[ if type_.typename == "float"]@
@{
name = "float"
bound = 3.402823e+38
}@
@[ elif type_.typename == "double"]@
@{
name = "double"
bound = 1.7976931348623157e+308
}@
@[ end if]@
assert value >= -@(bound) and value <= @(bound), \
"The '@(member.name)' field must be a @(name) in [@(-bound), @(bound)]"
@[ end if]@
@[ else]@
False
Expand Down
20 changes: 20 additions & 0 deletions rosidl_generator_py/test/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ def test_basic_types():
setattr(msg, 'uint%d_value' % i, -1)
with pytest.raises(AssertionError):
setattr(msg, 'int%d_value' % i, 2**i)
with pytest.raises(AssertionError):
setattr(msg, 'float32_value', -3.5e+38)
with pytest.raises(AssertionError):
setattr(msg, 'float32_value', 3.5e+38)
with pytest.raises(AssertionError):
setattr(msg, 'float64_value', 1.8e+308)
with pytest.raises(AssertionError):
setattr(msg, 'float64_value', -1.8e+308)


def test_strings():
Expand Down Expand Up @@ -450,6 +458,10 @@ def test_arrays():
setattr(msg, 'uint64_values', [2**64, 1, 2])
with pytest.raises(AssertionError):
setattr(msg, 'uint64_values', [-1, 1, 2])
with pytest.raises(AssertionError):
setattr(msg, 'float32_values', [-3.5e+38, 0.0, 3.5e+38])
with pytest.raises(AssertionError):
setattr(msg, 'float64_values', [-1.8e+308, 0.0, 1.8e+308])


def test_bounded_sequences():
Expand Down Expand Up @@ -661,6 +673,10 @@ def test_bounded_sequences():
setattr(msg, 'uint64_values', [2**64, 1, 2])
with pytest.raises(AssertionError):
setattr(msg, 'uint64_values', [-1, 1, 2])
with pytest.raises(AssertionError):
setattr(msg, 'float32_values', [-3.5e+38, 0.0, 3.5e+38])
with pytest.raises(AssertionError):
setattr(msg, 'float64_values', [-1.8e+308, 0.0, 1.8e+308])


def test_unbounded_sequences():
Expand Down Expand Up @@ -800,6 +816,10 @@ def test_unbounded_sequences():
setattr(msg, 'uint64_values', [2**64, 1, 2])
with pytest.raises(AssertionError):
setattr(msg, 'uint64_values', [-1, 1, 2])
with pytest.raises(AssertionError):
setattr(msg, 'float32_values', [-3.5e+38, 0.0, 3.5e+38])
with pytest.raises(AssertionError):
setattr(msg, 'float64_values', [-1.8e+308, 0.0, 1.8e+308])


def test_slot_attributes():
Expand Down

0 comments on commit 7638b52

Please sign in to comment.