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

Copy attachments buffer when parsing summary [20078] #100

Merged
merged 2 commits into from
Dec 12, 2023

Conversation

juanlofer-eprosima
Copy link
Contributor

@juanlofer-eprosima juanlofer-eprosima commented Nov 30, 2023

MCAP C++ library was previously modified so that attachments (and metadata) are stored in a class attribute, accessible through public methods, while parsing a file. However, attachments were not well stored as their internal buffer was not being copied (only their address), and after parsing this memory could be deallocated or reused in posterior actions.

Related:

@juanlofer-eprosima juanlofer-eprosima changed the title Copy attachments buffer when parsing summary Copy attachments buffer when parsing summary [20078] Dec 1, 2023
Signed-off-by: Juan Lopez Fernandez <juanlopez@eprosima.com>
@codecov-commenter
Copy link

codecov-commenter commented Dec 12, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (06d37fe) 20.36% compared to head (91da985) 20.19%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #100      +/-   ##
==========================================
- Coverage   20.36%   20.19%   -0.18%     
==========================================
  Files          29       29              
  Lines        2715     2714       -1     
  Branches     1423     1423              
==========================================
- Hits          553      548       -5     
- Misses       1694     1704      +10     
+ Partials      468      462       -6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Attachment attachment_copy = attachment;
attachment_copy.data = (std::byte*)std::malloc(attachment.dataSize);
std::memcpy((void*)attachment_copy.data, attachment.data, attachment.dataSize);
attachments_.emplace(attachment_copy.name, attachment_copy);
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider having std::move(attachment_copy) to avoid copying the attachment_copy unnecessarily when emplicing it in the attachment_.

// Copy buffer for storage as original is freed when leaving scope
Attachment attachment_copy = attachment;
attachment_copy.data = (std::byte*)std::malloc(attachment.dataSize);
std::memcpy((void*)attachment_copy.data, attachment.data, attachment.dataSize);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the (void *) casting necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe that is the case, I suppose I added it for a reason.

Copy link
Contributor

Choose a reason for hiding this comment

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

You're right. It's required.

}
attachments_.clear();
}

Copy link
Contributor

@Tempate Tempate Dec 12, 2023

Choose a reason for hiding this comment

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

The function .clear() calls the destructor of each element inside the map. Wouldn't it make more sense to override the destructor of the Attachment object to free attachment.data? In that way you would avoid having a clear_attachments_ function and simply call attachments_.clear() like with every other object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think that would be valid, because elsewhere (every place other than when destroying our added map) it would be calling a free that is already called somewhere else (there is no way of checking if already freed easily, perhaps adding an attribute, but too involved and risky).

Another option would be to inherit from Attachment and add that destructor only there (and have a map of children). I would not go for that, our changes to the mcap library should be minimal.

Signed-off-by: Juan Lopez Fernandez <juanlopez@eprosima.com>
Copy link
Contributor

@Tempate Tempate left a comment

Choose a reason for hiding this comment

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

LGTM

@rsanchez15 rsanchez15 merged commit 70097cc into main Dec 12, 2023
16 of 17 checks passed
@rsanchez15 rsanchez15 deleted the hotfix/copy-attachments-buffer branch December 12, 2023 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants