Skip to content

Commit 7283329

Browse files
Check remaining size before resizing sequences (#130)
* Check remaining size before resizing sequences Signed-off-by: Miguel Company <[email protected]> * Avoid linter complaining of too long function. Signed-off-by: Miguel Company <[email protected]> --------- Signed-off-by: Miguel Company <[email protected]>
1 parent 22667dc commit 7283329

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

rosidl_typesupport_fastrtps_c/resource/msg__type_support_c.cpp.em

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,16 @@ else:
356356
uint32_t cdrSize;
357357
cdr >> cdrSize;
358358
size_t size = static_cast<size_t>(cdrSize);
359+
360+
// Check there are at least 'size' remaining bytes in the CDR stream before resizing
361+
auto old_state = cdr.get_state();
362+
bool correct_size = cdr.jump(size);
363+
cdr.set_state(old_state);
364+
if (!correct_size) {
365+
fprintf(stderr, "sequence size exceeds remaining buffer\n");
366+
return false;
367+
}
368+
359369
if (ros_message->@(member.name).data) {
360370
@(array_fini)(&ros_message->@(member.name));
361371
}

rosidl_typesupport_fastrtps_cpp/resource/msg__type_support.cpp.em

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,16 @@ cdr_deserialize(
235235
uint32_t cdrSize;
236236
cdr >> cdrSize;
237237
size_t size = static_cast<size_t>(cdrSize);
238+
239+
// Check there are at least 'size' remaining bytes in the CDR stream before resizing
240+
auto old_state = cdr.get_state();
241+
bool correct_size = cdr.jump(size);
242+
cdr.set_state(old_state);
243+
if (!correct_size) {
244+
fprintf(stderr, "sequence size exceeds remaining buffer\n");
245+
return false;
246+
}
247+
238248
ros_message.@(member.name).resize(size);
239249
@[ if isinstance(member.type.value_type, BasicType) and member.type.value_type.typename not in ('boolean', 'wchar')]@
240250
if (size > 0) {
@@ -296,7 +306,7 @@ cdr_deserialize(
296306

297307
@[end for]@
298308
return true;
299-
}
309+
} // NOLINT(readability/fn_size)
300310

301311
@{
302312

0 commit comments

Comments
 (0)