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

cpp: Fix for undefined behavior in mcap::ParseByteArray(..) #1239

Conversation

MichaelOrlov
Copy link
Contributor

Changelog

Fix for possible undefined behavior in the cpp mcap reader.

Docs

None

Description

BeforeAfter

Undefined behavior when reading empty message definitions

No undefined behavior when reading empty message definitions

- According to the https://en.cppreference.com/w/cpp/string/byte/memcpy
If either dest or src is an invalid or null pointer, the behavior is
undefined, even if count is zero.
We have a nullptr in dest and count is zero in std::memcpy(..) when
message definition was recorded with empty strings.

Signed-off-by: Michael Orlov <michael.orlov@apex.ai>
Copy link
Member

@jtbandes jtbandes left a comment

Choose a reason for hiding this comment

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

👍 seems correct, could you also add a comment to describe why the condition is necessary? It's not obvious from the code. Relatedly, I also see this in vector::data(): "If size() is ​0​, data() may or may not return a null pointer." So the comment could be something like "data() may return nullptr if output is empty, but memcpy() does not accept nullptr".

Signed-off-by: Michael Orlov <michael.orlov@apex.ai>
@MichaelOrlov
Copy link
Contributor Author

@jtbandes Thanks for the review. I've added a comment with an explanation before if (size > 0) check.

@jtbandes jtbandes merged commit 6715481 into foxglove:main Sep 24, 2024
23 checks passed
@jtbandes
Copy link
Member

=> #1241

jtbandes added a commit that referenced this pull request Sep 25, 2024
Bump version to 1.4.1. Changes include:

- #1239 
- #1199
@MichaelOrlov MichaelOrlov deleted the morlov/fix-for-undefined-behavior-in-parse_byte_array branch September 27, 2024 23:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants