Fix deserialization of maps into non-empty maps (backport #340) - #342
Merged
Conversation
* 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
approved these changes
Sep 7, 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
As reported in #339,
Cdr::deserialize(std::map<K, T>&)presents two problems when the output map is not empty:DataReader::take_next_sample()) silently kept stale keys and values.NotEnoughMemoryExceptionthrown 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:
@Mergifyio backport 2.3.x
Fixes #339
Contributor Checklist
versions.mdfile (if applicable). Bug fix, no new feature.2.3.xis affected (reported against 2.3.6).Reviewer Checklist
This is an automatic backport of pull request #340 done by [Mergify](https://mergify.com).