Skip to content

Commit 7638b52

Browse files
add missing float/double bounds check (#128)
* add missing float/double bounds check Signed-off-by: Seulbae Kim <squizz617@gmail.com> Co-authored-by: Chris Lalancette <clalancette@openrobotics.org>
1 parent c6272b8 commit 7638b52

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

rosidl_generator_py/resource/_msg.py.em

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,22 @@ bound = 2**nbits
497497
@[ elif isinstance(type_, BasicType) and type_.typename == 'char']@
498498
all(val >= 0 and val) < 256 for val in value)), \
499499
@{assert_msg_suffixes.append('and each char in [0, 255]')}@
500+
@[ elif isinstance(type_, BasicType) and type_.typename in FLOATING_POINT_TYPES]@
501+
@[ if type_.typename == "float"]@
502+
@{
503+
name = "float"
504+
bound = 3.402823e+38
505+
}@
506+
all(val >= -@(bound) and val <= @(bound) for val in value)), \
507+
@{assert_msg_suffixes.append('and each float in [%f, %f]' % (-bound, bound))}@
508+
@[ elif type_.typename == "double"]@
509+
@{
510+
name = "double"
511+
bound = 1.7976931348623157e+308
512+
}@
513+
all(val >= -@(bound) and val <= @(bound) for val in value)), \
514+
@{assert_msg_suffixes.append('and each double in [%f, %f]' % (-bound, bound))}@
515+
@[ end if]@
500516
@[ else]@
501517
True), \
502518
@[ end if]@
@@ -538,6 +554,20 @@ bound = 2**nbits
538554
}@
539555
assert value >= 0 and value < @(bound), \
540556
"The '@(member.name)' field must be an unsigned integer in [0, @(bound - 1)]"
557+
@[ elif type_.typename in FLOATING_POINT_TYPES]@
558+
@[ if type_.typename == "float"]@
559+
@{
560+
name = "float"
561+
bound = 3.402823e+38
562+
}@
563+
@[ elif type_.typename == "double"]@
564+
@{
565+
name = "double"
566+
bound = 1.7976931348623157e+308
567+
}@
568+
@[ end if]@
569+
assert value >= -@(bound) and value <= @(bound), \
570+
"The '@(member.name)' field must be a @(name) in [@(-bound), @(bound)]"
541571
@[ end if]@
542572
@[ else]@
543573
False

rosidl_generator_py/test/test_interfaces.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ def test_basic_types():
111111
setattr(msg, 'uint%d_value' % i, -1)
112112
with pytest.raises(AssertionError):
113113
setattr(msg, 'int%d_value' % i, 2**i)
114+
with pytest.raises(AssertionError):
115+
setattr(msg, 'float32_value', -3.5e+38)
116+
with pytest.raises(AssertionError):
117+
setattr(msg, 'float32_value', 3.5e+38)
118+
with pytest.raises(AssertionError):
119+
setattr(msg, 'float64_value', 1.8e+308)
120+
with pytest.raises(AssertionError):
121+
setattr(msg, 'float64_value', -1.8e+308)
114122

115123

116124
def test_strings():
@@ -450,6 +458,10 @@ def test_arrays():
450458
setattr(msg, 'uint64_values', [2**64, 1, 2])
451459
with pytest.raises(AssertionError):
452460
setattr(msg, 'uint64_values', [-1, 1, 2])
461+
with pytest.raises(AssertionError):
462+
setattr(msg, 'float32_values', [-3.5e+38, 0.0, 3.5e+38])
463+
with pytest.raises(AssertionError):
464+
setattr(msg, 'float64_values', [-1.8e+308, 0.0, 1.8e+308])
453465

454466

455467
def test_bounded_sequences():
@@ -661,6 +673,10 @@ def test_bounded_sequences():
661673
setattr(msg, 'uint64_values', [2**64, 1, 2])
662674
with pytest.raises(AssertionError):
663675
setattr(msg, 'uint64_values', [-1, 1, 2])
676+
with pytest.raises(AssertionError):
677+
setattr(msg, 'float32_values', [-3.5e+38, 0.0, 3.5e+38])
678+
with pytest.raises(AssertionError):
679+
setattr(msg, 'float64_values', [-1.8e+308, 0.0, 1.8e+308])
664680

665681

666682
def test_unbounded_sequences():
@@ -800,6 +816,10 @@ def test_unbounded_sequences():
800816
setattr(msg, 'uint64_values', [2**64, 1, 2])
801817
with pytest.raises(AssertionError):
802818
setattr(msg, 'uint64_values', [-1, 1, 2])
819+
with pytest.raises(AssertionError):
820+
setattr(msg, 'float32_values', [-3.5e+38, 0.0, 3.5e+38])
821+
with pytest.raises(AssertionError):
822+
setattr(msg, 'float64_values', [-1.8e+308, 0.0, 1.8e+308])
803823

804824

805825
def test_slot_attributes():

0 commit comments

Comments
 (0)