Skip to content

Commit 09e5a5f

Browse files
Check remaining size before resizing sequences (#827)
Signed-off-by: Miguel Company <[email protected]>
1 parent 4e84d9d commit 09e5a5f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

rmw_fastrtps_dynamic_cpp/src/TypeSupport_impl.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,23 @@
4343
namespace rmw_fastrtps_dynamic_cpp
4444
{
4545

46+
/*!
47+
* Throws eprosima::fastcdr::exception::NotEnoughMemoryException if the remaining
48+
* input buffer is insufficient to read the given size.
49+
*/
50+
static void check_sequence_size(
51+
const size_t size,
52+
eprosima::fastcdr::Cdr & deser)
53+
{
54+
auto state = deser.get_state();
55+
bool correct_size = deser.jump(size);
56+
deser.set_state(state);
57+
if (!correct_size) {
58+
throw eprosima::fastcdr::exception::NotEnoughMemoryException(
59+
"Insufficent input buffer for sequence length");
60+
}
61+
}
62+
4663
template<typename T>
4764
struct GenericCSequence;
4865

@@ -716,6 +733,7 @@ inline void deserialize_field<std::wstring>(
716733
size = static_cast<uint32_t>(member->array_size_);
717734
} else {
718735
deser >> size;
736+
check_sequence_size(size, deser);
719737
member->resize_function(field, size);
720738
}
721739
for (size_t i = 0; i < size; ++i) {
@@ -740,6 +758,7 @@ void deserialize_field(
740758
auto & data = *reinterpret_cast<typename GenericCSequence<T>::type *>(field);
741759
int32_t dsize = 0;
742760
deser >> dsize;
761+
check_sequence_size(dsize, deser);
743762
GenericCSequence<T>::init(&data, dsize);
744763
deser.deserialize_array(reinterpret_cast<T *>(data.data), dsize);
745764
}
@@ -887,6 +906,7 @@ bool TypeSupport<MembersType>::deserializeROSmessage(
887906
uint32_t num_elems = 0;
888907
deser >> num_elems;
889908
array_size = static_cast<size_t>(num_elems);
909+
check_sequence_size(array_size, deser);
890910

891911
if (!member->resize_function) {
892912
RMW_SET_ERROR_MSG("unexpected error: resize function is null");

0 commit comments

Comments
 (0)