Skip to content

[25811] try-construct support - #350

Merged
richiware merged 8 commits into
masterfrom
feature/try_construct
Nov 11, 2026
Merged

richiware merged 8 commits into
masterfrom
feature/try_construct

Conversation

@richiware

@richiware richiware commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Description

This PR adds try-construct support to Cdr deserialization, as defined by DDS-XTypes TryConstruct annotations. The
serialized data may hold more characters, elements or entries than the destination type accepts. A new set of deserialize
overloads takes a CdrTryConstructFlag policy, which decides what happens in that case:

Policy Behavior
CdrTryConstructFlag::FAIL Throws BadParamException. The stream is left where it was before the call, and maps keep
their previous content.
CdrTryConstructFlag::DEFAULT_VALUE The value is consumed and the destination is left empty.
CdrTryConstructFlag::TRIM The value is consumed and the destination keeps the first characters, elements or entries
that fit.

New overloads, all taking a CdrTryConstructFlag (and a max_length where the type has no implicit bound):

  • char*&, wchar_t*&, std::string&, std::wstring& and fixed_string<MAX_CHARS>&.
  • std::vector<_T>& for primitive, enumerated and non-primitive elements, and std::vector<bool>&.
  • std::map<_K, _T>& for primitive and non-primitive values. As with the regular overloads, the destination map is only
    replaced 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:

  • In XCDRv2, non-primitive collections skip them using the DHEADER.
  • Otherwise, they are decoded in a fake mode, which advances through the data without storing the decoded values.

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 such
value it decodes itself.

deserialize_framing() is public, so that code decoding a type whose layout depends on a value it reads can use it. Fast
DDS-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

  • ABI: Cdr gains a data member (fake_mode), which changes sizeof(Cdr). This breaks ABI with 2.4.0 and must be
    called out in the release notes.
  • Behavior: deserialize(fixed_string<MAX_CHARS>&) now throws BadParamException when the serialized string is longer
    than MAX_CHARS, instead of truncating it silently. deserialize(fixed_string<MAX_CHARS>&, CdrTryConstructFlag::TRIM) keeps
    the old truncating behavior.
  • API: all other changes are additive. The private exported read_string(uint32_t&) and read_wstring(uint32_t&)
    symbols are kept, because binaries built against 2.4.0 reference them from inlined header code.
  • Generated code: unions generated by previous Fast DDS-Gen versions read their discriminator with operator>>. Until
    they are regenerated, they are not consumed correctly when discarded from a sequence or map under XCDRv1.

Tests

New test/xcdr/type_consistency.cpp covers every overload and policy, for XCDRv1 and XCDRv2 in both endiannesses. It
includes:

  • Discarding elements whose size is only known by decoding them: strings, nested sequences, mutable structures, structures
    with optional members and unions. Each test serializes a trailer after the collection and checks that it is read back
    unchanged.
  • FAIL leaving the stream and the destination map unchanged, and maps keeping their content on error.
  • XCDRv2 DHEADER mismatches.

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

  • Commit messages follow the project guidelines.
  • The code follows the style guidelines of this project.
  • Tests that thoroughly check the new feature have been added/Regression tests checking the bug and its fix have been
    added; the added tests pass locally
  • Any new/modified methods have been properly documented using Doxygen.
  • ❌ Changes are backport compatible: they do NOT break ABI nor change library core behavior. New feature: sizeof(Cdr)
    changes and deserialize(fixed_string<MAX_CHARS>&) now throws on oversized strings (see Compatibility notes).
  • Changes are API compatible.
  • ❌ New feature has been added to the versions.md file (if applicable). Pending.
  • N/A Applicable backports have been included in the description.

Reviewer Checklist

  • The PR has a milestone assigned.
  • The title and description correctly express the PR's purpose.
  • Check contributor checklist is correct.
  • Check CI results: changes do not issue any warning.
  • Check CI results: CI pass and failing tests are unrelated with the changes.

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>
@richiware richiware changed the title Feature/try construct try-construct support Oct 27, 2026
@richiware richiware changed the title try-construct support [25811] try-construct support Oct 27, 2026
@richiware richiware added this to the v2.5.0 milestone Oct 27, 2026
@MiguelCompany
MiguelCompany self-requested a review October 28, 2026 10:17
@Danipiza
Danipiza self-requested a review November 3, 2026 08:04
Comment thread include/fastcdr/Cdr.h Resolved
Comment thread include/fastcdr/Cdr.h Resolved
Comment thread include/fastcdr/Cdr.h Resolved
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>
@richiware

Copy link
Copy Markdown
Member Author

🏓

@Danipiza Danipiza left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM, waiting for green CI in [24344] Type Assignability - #39

@richiware
richiware requested a review from Danipiza November 6, 2026 08:52
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
richiware force-pushed the feature/try_construct branch from 48f0a80 to dc2bc14 Compare November 11, 2026 07:54
@richiware
richiware requested review from Danipiza and removed request for Danipiza November 11, 2026 07:55
@richiware
richiware removed the request for review from MiguelCompany November 11, 2026 08:03
@richiware
richiware merged commit 7a23787 into master Nov 11, 2026
16 of 24 checks passed
@richiware
richiware deleted the feature/try_construct branch November 11, 2026 08:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants