Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add missing float/double bounds check #128

Merged
merged 5 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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