Fix IDLObjectSequence#calculateSizeBytes for variable-size elements - #103
Merged
ds58 merged 2 commits intoJul 22, 2026
Merged
Conversation
IDLSequence#calculateSizeBytes's generic loop assumes elementSizeBytes() returns a size that is independent of the alignment context it was given, and separately computes+adds the leading alignment padding before each element via CDRBuffer.alignment(currentAlignment, elementSizeBytes). That holds for every fixed-size primitive sequence (e.g. IDLDoubleSequence, whose element size is always the constant 8, itself a valid CDR alignment boundary). It does not hold for IDLObjectSequence: its elementSizeBytes() forwards to elements[i].calculateSizeBytes(currentAlignment), and a well-behaved CDRSerializable already bakes its own leading alignment padding into that call. Reusing the generic loop on top double-counts alignment, and does so incoherently, since CDRBuffer#alignment assumes its `bytes` argument is itself a valid power-of-two CDR boundary (1/2/4/8) - never true for an arbitrary struct's total encoded size. Found while debugging why a two-element tf2_msgs.TFMessage.transforms sequence (geometry_msgs.TransformStamped[], each element containing a Header + string + Transform) computed a size tens of bytes off from what serialize() actually wrote. This matters because ROS2Publisher#writeAndPublish uses calculateSizeBytes() as the exact number of bytes to resize/copy to the outgoing DDS payload - any struct-sequence message (TFMessage, MarkerArray, DiagnosticArray, ...) published today over-copies whatever was previously in the write buffer as trailing garbage. Changes: - IDLObjectSequence#calculateSizeBytes: override with a version that doesn't double-apply alignment, verified to exactly match what serialize() writes (see the new regression test). - ROS2Publisher#writeAndPublish: defense in depth - use the buffer's actual post-serialize() position for the copied payload length instead of trusting calculateSizeBytes()'s estimate, so any future CDRSerializable whose two methods drift out of sync fails safe instead of shipping garbage/truncated bytes. - Added IDLObjectSequenceVariableSizeElementTest: IDLObjectSequenceTest's TestIDLMsg fixture is a fixed 4 bytes per element (already a valid CDR alignment boundary), so it can't exercise this bug; the new fixture's size varies with a string field, matching the shape that triggered it.
Contributor
|
Thanks, I simplified the javadoc so it's a bit quicker to read |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
IDLSequence#calculateSizeBytes's generic loop assumeselementSizeBytes()returns a size that is independent of the alignment context it was given, and separately computes+adds the leading alignment padding before each element viaCDRBuffer.alignment(currentAlignment, elementSizeBytes). That holds for every fixed-size primitive sequence (e.g.IDLDoubleSequence, whose element size is always the constant8, itself a valid CDR alignment boundary).It does not hold for
IDLObjectSequence: itselementSizeBytes()forwards toelements[i].calculateSizeBytes(currentAlignment), and a well-behavedCDRSerializablealready bakes its own leading alignment padding into that call. Reusing the generic loop on top double-counts alignment, and does so incoherently, sinceCDRBuffer#alignmentassumes itsbytesargument is itself a valid power-of-two CDR boundary (1/2/4/8) - never true for an arbitrary struct's total encoded size.Found while debugging why a two-element
tf2_msgs.TFMessage.transformssequence (geometry_msgs.TransformStamped[], each element containing aHeader+string+Transform) computed a size tens of bytes off from whatserialize()actually wrote. This matters becauseROS2Publisher#writeAndPublishusescalculateSizeBytes()as the exact number of bytes to resize/copy to the outgoing DDS payload - any struct-sequence message (TFMessage,MarkerArray,DiagnosticArray, ...) published today over-copies whatever was previously in the write buffer as trailing garbage.Changes
IDLObjectSequence#calculateSizeBytes: override with a version that doesn't double-apply alignment, verified to exactly match whatserialize()writes (see the new regression test).ROS2Publisher#writeAndPublish: defense in depth - use the buffer's actual post-serialize()position for the copied payload length instead of trustingcalculateSizeBytes()'s estimate, so any futureCDRSerializablewhose two methods drift out of sync fails safe instead of shipping garbage/truncated bytes.IDLObjectSequenceVariableSizeElementTest:IDLObjectSequenceTest'sTestIDLMsgfixture is a fixed 4 bytes per element (already a valid CDR alignment boundary), so it can't exercise this bug; the new fixture's size varies with a string field, matching the shape that triggered it.Test plan
./gradlew :jros2-test:test- full suite: 763 tests, only the pre-existingAsyncROS2Testflake fails (reproduces identically ondevelopwith these changes stashed out, so it's unrelated)IDLObjectSequenceVariableSizeElementTestpasses:calculateSizeBytes()now exactly matchesserialize()'s actual output, and a deserialize round-trip is verifiedIDLObjectSequenceTest,IDLDoubleSequenceTest,IDLIntSequenceTest, etc. tests still pass unchangedROS2PublisherTestsuite still passes