Repository navigation
[25811] try-construct support - #350
Merged
Merged
Conversation
Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>
Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>
Discarded elements are decoded in fake mode, which skips storing every primitive. That included the values that decide how many bytes an element occupies, so discarding a string, a nested sequence, a mutable structure or a structure with an optional member left the stream misaligned. Add a private deserialize_framing() helper that always stores the value, also in fake mode. Use it for every library-owned length or header read: string and sequence lengths, map lengths, DHEADERs, XCDRv1 parameter headers, XCDRv2 EMHEADERs and the optional presence flag. Fake mode is now set through a fake_mode_scope guard, which restores the previous value also when an exception other than eprosima::fastcdr::exception::Exception escapes. Validate the DHEADER bounds in the try-construct XCDRv2 sequence and map overloads, and restore the stream state when they throw. Restore the one-argument read_string() and read_wstring() overloads that binaries built against 2.4.0 reference from inlined header code. They forward to the new overloads with no length limit. Make CdrTryConstructFlag a scoped enum, so its enumerators do not leak into the eprosima::fastcdr namespace. Add tests that serialize a trailer after the collection and check it is read back unchanged after discarding elements of each kind. Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>
Reject XCDRv2 sequences and maps whose DHEADER does not match their elements. Before, a DHEADER that ran out before all the kept elements were read left the rest default-constructed. Kept elements that overran the DHEADER while trimming were accepted. FAIL now restores the stream to where it was before the call, as every other exception path does. The documentation no longer claims the stream is always fully consumed. The try-construct map overloads deserialize into a temporary map and only replace the destination when the whole deserialization succeeds, as the regular map overloads do. The TRIM documentation now matches the behaviour: the first max_length entries in wire order are kept. DEFAULT_VALUE and TRIM to zero characters on char* and wchar_t* now return an allocated empty string, as the std::string and std::wstring overloads do. FAIL leaves the pointer unchanged. Fix the plain wchar_t* deserialization not moving past the characters in fake mode. Add Doxygen for the non-primitive sequence and map overloads, use a map specific FAIL message, remove the dead policy switch in the fixed_string overload and a stray blank line. Add tests for FAIL leaving the stream and destination unchanged, for maps keeping their content on error, and for XCDRv2 DHEADER mismatches. Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>
Elements discarded by a try-construct policy are consumed in fake mode, where the regular deserialize overloads do not store the decoded values. Generated unions read their discriminator with operator>>, so a discarded union kept the default discriminator, decoded the wrong member and left the stream misaligned without raising any error. This affected XCDRv1, where discarded non-primitive elements are consumed one by one. Move deserialize_framing to the public API, documenting that code decoding a type whose layout depends on a value it reads, like a union discriminator, must decode that value with it. Fast DDS-Gen generated code is updated to use it for union discriminators. Add tests with a union-like type, (de)serialized as Fast DDS-Gen generates it, checking that trimming or discarding a sequence of unions leaves the stream just past the sequence. Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>
MiguelCompany
self-requested a review
October 28, 2026 10:17
Danipiza
self-requested a review
November 3, 2026 08:04
Danipiza
reviewed
Nov 3, 2026
Danipiza
requested changes
Nov 3, 2026
The try-construct overload for XCDRv2 sequences of non-primitive types resized the destination before checking that the announced length fits in the DHEADER. A malformed eight-byte input with a DHEADER of 4 and a length of 1000000 allocated a million elements, then threw BadParamException from the per-element DHEADER check. The regular overload rejects that input without allocating. Check that the length does not exceed the bytes left in the DHEADER, as the regular overload does, since every element takes at least one byte. The check runs before the policy is applied, so DEFAULT_VALUE and TRIM reject the malformed sequence too, and it restores the stream before throwing NotEnoughMemoryException. Move the resize inside the try block, as in the regular overload. Add a test with that input, checking for every policy that the call throws NotEnoughMemoryException, allocates nothing and leaves the stream at its start. Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>
The try-construct overload for XCDRv2 sequences of non-primitive types returned as soon as it read a length of 0, without checking that the bytes read matched the DHEADER. With a DHEADER larger than 4, the unread bytes stayed in the stream and later reads started at the wrong offset. The regular overload rejects that input. Remove the early return so empty sequences go through the final DHEADER check. That check throws BadParamException and restores the stream when the bytes read differ from the DHEADER. A valid empty sequence, with a DHEADER of 4, still returns an empty vector. Add a test with a DHEADER of 8 and a length of 0, checking for every policy that the call throws BadParamException, leaves the destination empty and leaves the stream at its start. Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>
Member
Author
|
🏓 |
Danipiza
approved these changes
Nov 5, 2026
Danipiza
left a comment
There was a problem hiding this comment.
LGTM, waiting for green CI in [24344] Type Assignability - #39
Danipiza
approved these changes
Nov 11, 2026
Place the fake deserialization flag right after encapsulation_serialized_ so it fits in the padding before context_, keeping sizeof(Cdr) unchanged with respect to master. Rename it to fake_mode_ to match the naming of the other members. Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>
richiware
force-pushed
the
feature/try_construct
branch
from
November 11, 2026 07:54
48f0a80 to
dc2bc14
Compare
Danipiza
approved these changes
Nov 11, 2026
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.
Description
This PR adds try-construct support to
Cdrdeserialization, as defined by DDS-XTypesTryConstructannotations. Theserialized data may hold more characters, elements or entries than the destination type accepts. A new set of
deserializeoverloads takes a
CdrTryConstructFlagpolicy, which decides what happens in that case:CdrTryConstructFlag::FAILBadParamException. The stream is left where it was before the call, and maps keepCdrTryConstructFlag::DEFAULT_VALUECdrTryConstructFlag::TRIMNew overloads, all taking a
CdrTryConstructFlag(and amax_lengthwhere the type has no implicit bound):char*&,wchar_t*&,std::string&,std::wstring&andfixed_string<MAX_CHARS>&.std::vector<_T>&for primitive, enumerated and non-primitive elements, andstd::vector<bool>&.std::map<_K, _T>&for primitive and non-primitive values. As with the regular overloads, the destination map is onlyreplaced when the whole deserialization succeeds.
The serialized wire format is not changed. Only the deserialization side is affected.
Discarding elements
The elements and entries that do not fit are consumed from the stream without being stored:
Some values decide how many bytes an element occupies: string and sequence lengths, DHEADERs, member headers, presence flags
and union discriminators. These are always stored, through
Cdr::deserialize_framing(). Fast CDR uses it for every suchvalue it decodes itself.
deserialize_framing()is public, so that code decoding a type whose layout depends on a value it reads can use it. FastDDS-Gen now uses it for union discriminators (eProsima/Fast-DDS-Gen#XX).
Stricter XCDRv2 checks
The try-construct overloads for non-primitive sequences and maps reject a DHEADER that is out of bounds. They also reject one
that does not match the elements read, whether it is too small for the elements announced or overrun by the elements kept.
Compatibility notes
Cdrgains a data member (fake_mode), which changessizeof(Cdr). This breaks ABI with 2.4.0 and must becalled out in the release notes.
deserialize(fixed_string<MAX_CHARS>&)now throwsBadParamExceptionwhen the serialized string is longerthan
MAX_CHARS, instead of truncating it silently.deserialize(fixed_string<MAX_CHARS>&, CdrTryConstructFlag::TRIM)keepsthe old truncating behavior.
read_string(uint32_t&)andread_wstring(uint32_t&)symbols are kept, because binaries built against 2.4.0 reference them from inlined header code.
operator>>. Untilthey are regenerated, they are not consumed correctly when discarded from a sequence or map under XCDRv1.
Tests
New
test/xcdr/type_consistency.cppcovers every overload and policy, for XCDRv1 and XCDRv2 in both endiannesses. Itincludes:
with optional members and unions. Each test serializes a trailer after the collection and checks that it is read back
unchanged.
FAILleaving the stream and the destination map unchanged, and maps keeping their content on error.This PR depends on Fast DDS-Gen generated code changes in eProsima/Fast-DDS-Gen#XX, and the two must be released together.
Contributor Checklist
added; the added tests pass locally
sizeof(Cdr)changes and
deserialize(fixed_string<MAX_CHARS>&)now throws on oversized strings (see Compatibility notes).versions.mdfile (if applicable). Pending.Reviewer Checklist