Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

fix capacity of serialized messages #58

Merged
merged 1 commit into from
Jul 31, 2020

Conversation

dirk-thomas
Copy link
Member

@dirk-thomas dirk-thomas commented Jul 28, 2020

Since the code was added originally in ros2/rmw_connext#259 the buffer_capacity of the rcutils_uint8_array_t wasn't updated after being reallocated. See http://build.ros2.org/view/Rci/job/Rci__nightly-connext_ubuntu_focal_amd64/29/testReport/rclcpp/TestSerializedMessage/serialization/ for a failing test because of that.

The patch is a bit more length since it makes sure to not partially modify the passed in rcutils_uint8_array_t when exiting with an error.

Signed-off-by: Dirk Thomas <dirk-thomas@users.noreply.github.com>
Copy link
Contributor

@Karsten1987 Karsten1987 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a first pass over it. Will look at it again in more depth.

Comment on lines +460 to +468
if (cdr_stream->buffer_capacity < expected_length) {
uint8_t * new_buffer = static_cast<uint8_t *>(cdr_stream->allocator.allocate(expected_length, cdr_stream->allocator.state));
if (NULL == new_buffer) {
fprintf(stderr, "failed to allocate memory for cdr data\n");
return false;
}
cdr_stream->allocator.deallocate(cdr_stream->buffer, cdr_stream->allocator.state);
cdr_stream->buffer = static_cast<uint8_t *>(cdr_stream->allocator.allocate(cdr_stream->buffer_length, cdr_stream->allocator.state));
cdr_stream->buffer = new_buffer;
cdr_stream->buffer_capacity = expected_length;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not using rcutils_uint8_array_resize here?
https://github.com/ros2/rcutils/blob/master/src/uint8_array.c#L74

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't plan to refactor existing code - just fix the failing test / broken logic.

I would suggest refactoring it in a follow up PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it has anything to do with refactoring. The introduced code here is really the same as the one in rcutils: https://github.com/ros2/rcutils/blob/master/src/uint8_array.c#L91-L98 which also takes care of resetting the length and capacity in case the allocation fails.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The introduced code here is really the same as the one in rcutils:

My patch doesn't introduce much at all. It essentially just adds the missing cdr_stream->buffer_capacity = expected_length; assignment.

Also using rcutils_uint8_array_resize implies that the original buffer is altered in case the reallocation fails which is neither necessary nor (imo) desired.

@Karsten1987
Copy link
Contributor

If we set the buffer capacity here, we should most likely revert this change here: https://github.com/ros2/rmw_connext/pull/417/files

if (@(__dds_cpp_msg_type_prefix)_Plugin_serialize_to_cdr_buffer(
reinterpret_cast<char *>(cdr_stream->buffer),
&buffer_length_uint,
&dds_message) != RTI_TRUE)
{
cdr_stream->buffer_length = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we print an error message above when reallocation fails, I think we can add an error message here as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also unrelated to the change in this PR. I don't know why originally no error message was printed here - maybe that was intentional? Please address this in a separate PR if you think it should output an error message.

@dirk-thomas
Copy link
Member Author

If we set the buffer capacity here, we should most likely revert this change here: https://github.com/ros2/rmw_connext/pull/417/files

Please fill a separate issue for this if you think that code needs to be changed. It doesn't seem related to me.

Copy link
Contributor

@Karsten1987 Karsten1987 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go ahead and merge if it helps to get the buildfarm green

@dirk-thomas
Copy link
Member Author

dirk-thomas commented Jul 30, 2020

CI builds with only Connext:

  • Linux Build Status
    • Same test failures present on master (minus some in the meantime resolved ones): Build Status
  • macOS Build Status
    • Same test failures present on master (minus some in the meantime resolved ones): Build Status
  • Windows Build Status
    • Same test failures present on master (minus some in the meantime resolved ones): Build Status

@dirk-thomas dirk-thomas merged commit 79bdebb into master Jul 31, 2020
@delete-merged-branch delete-merged-branch bot deleted the dirk-thomas/fix-capacity-of-serialized-messages branch July 31, 2020 05:40
@hidmic
Copy link
Contributor

hidmic commented Aug 12, 2020

If I'm not mistaken, this could be backported to Foxy.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants