Skip to content

Fix UB in accessing the keys#879

Merged
clalancette merged 2 commits intorollingfrom
clalancette/fix-key-crash
May 3, 2026
Merged

Fix UB in accessing the keys#879
clalancette merged 2 commits intorollingfrom
clalancette/fix-key-crash

Conversation

@clalancette
Copy link
Copy Markdown
Contributor

@clalancette clalancette commented Apr 30, 2026

Description

This fixes two different problems with respect to keys:

  • Make sure to do key.resize()

Doing a key.reserve() allocates memory, but still
leaves the size at 0. Thus an access with the []
operator is UB. This came up on Ubuntu 26.04 because
it is more strict about UB.

Fix it by using resize() instead, which does increase
the size and thus the [] operator is no longer UB.

  • rmw_fastrtps_dynamic_cpp: Fix a UB in key support.

The problem here is that we were always accessing 16 bytes
of data from the key buffer, even when it was shorter than
that. What we do instead is to always set the ihandle->value
to all zeros, then only copy in the number of key_buffer
bytes we have. This matches what rmw_fastrtps_cpp does,
and fixes a crash in tests on Ubuntu 26.04

This should be merged at approximately the same time as #880 , #881 , and ros2/system_tests#592

Is this user-facing behavior change?

No.

Did you use Generative AI?

Yes, Claude Opus 4.7

Additional Information

This should be backported to Lyrical and Kilted.

Doing a key.reserve() allocates memory, but still
leaves the size at 0.  Thus an access with the []
operator is UB.  This came up on Ubuntu 26.04 because
it is more strict about UB.

Fix it by using resize() instead, which does increase
the size and thus the [] operator is no longer UB.

Signed-off-by: Chris Lalancette <clalancette@gmail.com>
The problem here is that we were always accessing 16 bytes
of data from the key buffer, even when it was shorter than
that.  What we do instead is to always set the ihandle->value
to all zeros, then only copy in the number of key_buffer
bytes we have.  This matches what rmw_fastrtps_cpp does,
and fixes a crash in tests on Ubuntu 26.04

Signed-off-by: Chris Lalancette <clalancette@gmail.com>
@clalancette clalancette changed the title Clalancette/fix key crash Fix UB in accessing the keys Apr 30, 2026
Copy link
Copy Markdown
Collaborator

@MiguelCompany MiguelCompany left a comment

Choose a reason for hiding this comment

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

LGTM

@clalancette
Copy link
Copy Markdown
Contributor Author

Here is CI with this and all of the other PRs listed here:

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

Copy link
Copy Markdown
Collaborator

@fujitatomoya fujitatomoya left a comment

Choose a reason for hiding this comment

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

lgtm with green CI.

@clalancette
Copy link
Copy Markdown
Contributor Author

This one has ACKs and passes CI, so I'm going to merge it and backport it.

@clalancette clalancette merged commit eb15778 into rolling May 3, 2026
3 checks passed
@clalancette clalancette deleted the clalancette/fix-key-crash branch May 3, 2026 18:52
@clalancette
Copy link
Copy Markdown
Contributor Author

@Mergifyio backport lyrical kilted

@mergify
Copy link
Copy Markdown

mergify Bot commented May 3, 2026

backport lyrical kilted

✅ Backports have been created

Details

clalancette added a commit that referenced this pull request May 4, 2026
* Make sure to do key.resize()

Doing a key.reserve() allocates memory, but still
leaves the size at 0.  Thus an access with the []
operator is UB.  This came up on Ubuntu 26.04 because
it is more strict about UB.

Fix it by using resize() instead, which does increase
the size and thus the [] operator is no longer UB.



* rmw_fastrtps_dynamic_cpp: Fix a UB in key support.

The problem here is that we were always accessing 16 bytes
of data from the key buffer, even when it was shorter than
that.  What we do instead is to always set the ihandle->value
to all zeros, then only copy in the number of key_buffer
bytes we have.  This matches what rmw_fastrtps_cpp does,
and fixes a crash in tests on Ubuntu 26.04



---------


(cherry picked from commit eb15778)

Signed-off-by: Chris Lalancette <clalancette@gmail.com>
Co-authored-by: Chris Lalancette <clalancette@gmail.com>
clalancette added a commit that referenced this pull request May 5, 2026
* Make sure to do key.resize()

Doing a key.reserve() allocates memory, but still
leaves the size at 0.  Thus an access with the []
operator is UB.  This came up on Ubuntu 26.04 because
it is more strict about UB.

Fix it by using resize() instead, which does increase
the size and thus the [] operator is no longer UB.



* rmw_fastrtps_dynamic_cpp: Fix a UB in key support.

The problem here is that we were always accessing 16 bytes
of data from the key buffer, even when it was shorter than
that.  What we do instead is to always set the ihandle->value
to all zeros, then only copy in the number of key_buffer
bytes we have.  This matches what rmw_fastrtps_cpp does,
and fixes a crash in tests on Ubuntu 26.04



---------


(cherry picked from commit eb15778)

Signed-off-by: Chris Lalancette <clalancette@gmail.com>
Co-authored-by: Chris Lalancette <clalancette@gmail.com>
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.

3 participants