Skip to content

Fix capnproto add_value_to_map overwriting all entries at index 0#634

Open
lixin-wei wants to merge 1 commit intogetml:mainfrom
lixin-wei:fix/capnproto-add-value-to-map
Open

Fix capnproto add_value_to_map overwriting all entries at index 0#634
lixin-wei wants to merge 1 commit intogetml:mainfrom
lixin-wei:fix/capnproto-add-value-to-map

Conversation

@lixin-wei
Copy link
Contributor

@lixin-wei lixin-wei commented Mar 22, 2026

Summary

Fix a bug in capnproto's add_value_to_map, It doesn't use parent->ix++ to track the entry index. Instead, it creates a new OutputArrayType each time with ix_ defaulting to 0, so every entry overwrites index 0 in the capnproto list.

Test plan

  • New test capnproto.test_map_with_string_values fails before the fix (only last entry survives round-trip)
  • New test passes after the fix
  • All 46 existing capnproto tests continue to pass

add_value_to_map constructed a fresh OutputArrayType each call without
using _parent->ix_++, so ix_ always defaulted to 0 and every map entry
overwrote slot 0 in the Cap'n Proto entries list. Only the last entry
survived serialization; on read-back the remaining default entries (with
empty-string keys) triggered stoull errors.

Fix by using designated initializers with .ix_ = _parent->ix_++,
matching all other add_*_to_map methods in Writer.cpp. Also wrap the
key _name in std::string() so add_value_to_object deduces T as
std::string rather than std::string_view, which has no handling branch.

Add test_map_value_types to exercise maps with primitive value types
(std::map<std::string, std::string>) through add_value_to_map.

Made-with: Cursor
@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical bug in the Cap'n Proto Writer class that caused std::map<std::string, std::string> entries to be incorrectly serialized, resulting in data loss. The fix ensures that map entries are properly indexed and stored, and a new test case has been added to prevent regressions and validate the correct behavior of string-to-string map serialization.

Highlights

  • Bug Fix in add_value_to_map: Resolved an issue where Writer::add_value_to_map incorrectly overwrote all map entries at index 0 due to improper OutputArrayType initialization, leading to only the last entry being serialized.
  • Corrected Map Entry Indexing: Implemented designated initializers for OutputArrayType with .ix_ = _parent->ix_++ to ensure each map entry is written to a unique index, aligning with other add_*_to_map methods.
  • String Key Handling: Modified add_value_to_object calls to explicitly wrap std::string_view keys in std::string to ensure correct type deduction and serialization.
  • New Test Case Added: Introduced test_map_with_string_values to specifically validate the serialization and deserialization of std::map<std::string, std::string>, which previously failed before the fix.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request refactors the add_value_to_map function in Writer.hpp to correctly initialize OutputArrayType with an index (.ix_ = _parent->ix_++) and explicitly convert std::string_view keys to std::string for proper Cap'n Proto serialization. A new test file, test_map_value_types.cpp, was added to validate the serialization and deserialization of maps containing string values, confirming the intended functionality of the rfl::capnproto library.

@lixin-wei
Copy link
Contributor Author

lixin-wei commented Mar 22, 2026

The failed checks seems not related to my change. It's a network issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant