Skip to content

Fix deserialization of maps into non-empty maps (backport #340) - #341

Merged
MiguelCompany merged 1 commit into
2.3.xfrom
mergify/bp/2.3.x/pr-340
Sep 7, 2026
Merged

Fix deserialization of maps into non-empty maps (backport #340)#341
MiguelCompany merged 1 commit into
2.3.xfrom
mergify/bp/2.3.x/pr-340

Conversation

@mergify

@mergify mergify Bot commented Sep 7, 2026

Copy link
Copy Markdown

Description

As reported in #339, Cdr::deserialize(std::map<K, T>&) presents two problems when the output map is not empty:

  • The overload for primitive (arithmetic / enum) mapped types never cleared the output map, so deserializing into an already used map (e.g. a sample reused by DataReader::take_next_sample()) silently kept stale keys and values.
  • Both overloads modified the output map (cleared it and/or added elements) before completing the deserialization, so a NotEnoughMemoryException thrown inside the loop left the output map cleared or partially filled.

Following the suggestion in #339, both overloads now deserialize the elements into a new map, which is moved into the output one once the whole deserialization succeeds. This way the previous content of the output map is always replaced, and it is preserved if the deserialization fails.

Two commits:

  • First commit adds tests for correct/incorrect deserialization of primitive/non-primitive maps, including the case of reusing the same map for two deserialization calls. The tests fail on current master.
  • Second commit contains the fix. All tests pass locally (2118/2118 ctest).

@Mergifyio backport 2.3.x

Fixes #339

Contributor Checklist

  • Commit messages follow the project guidelines.
  • The code follows the style guidelines of this project.
  • 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.
  • Changes are API compatible.
  • N/A New feature has been added to the versions.md file (if applicable). Bug fix, no new feature.
  • Applicable backports have been included in the description. 2.3.x is affected (reported against 2.3.6).

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.

This is an automatic backport of pull request #340 done by [Mergify](https://mergify.com).

* Add tests for deserialization of maps into non-empty maps

Tests added for both primitive and non-primitive mapped types:
- Deserialization into a non-empty map.
- Reuse of the same target map in two consecutive deserializations.
- Content of the target map preserved when the deserialization fails.

These tests show the issues described in #339.

Signed-off-by: lijiapei <l2249712344@gmail.com>

* Fix deserialization of maps into non-empty maps

Deserialize into a new map and move it into the output one once the
whole deserialization succeeds.

This way the output map is not left cleared or partially filled when an
exception is thrown inside the deserialization loop, and the previous
content of a non-empty output map is always replaced. This applies to
both the primitive and non-primitive mapped type overloads.

Fixes #339.

Signed-off-by: lijiapei <l2249712344@gmail.com>

* Fix MSVC C4244 warning in map deserialization tests

Use explicit static_cast<uint16_t> for map keys, as done in
DeserializeIntoANonEmptyMapInXCDRv1. int literals converted to
uint16_t trigger warning C4244, treated as error by Windows CI.

Signed-off-by: lijiapei <l2249712344@gmail.com>

---------

Signed-off-by: lijiapei <l2249712344@gmail.com>
(cherry picked from commit b2457c4)
@MiguelCompany MiguelCompany added this to the v2.3.7 milestone Sep 7, 2026
@MiguelCompany
MiguelCompany self-requested a review September 7, 2026 08:31
@MiguelCompany
MiguelCompany merged commit b2a1738 into 2.3.x Sep 7, 2026
12 checks passed
@MiguelCompany
MiguelCompany deleted the mergify/bp/2.3.x/pr-340 branch September 7, 2026 08:49
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.

2 participants