Skip to content

Fix Cap'n Proto serialization of unsigned integer types#635

Open
lixin-wei wants to merge 1 commit intogetml:mainfrom
lixin-wei:fix/capnproto-uint64-serialization
Open

Fix Cap'n Proto serialization of unsigned integer types#635
lixin-wei wants to merge 1 commit intogetml:mainfrom
lixin-wei:fix/capnproto-uint64-serialization

Conversation

@lixin-wei
Copy link
Contributor

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

Summary

serializing

struct A {
  uint64_t x = UINT64_MAX;
}

in capnproto will throw:

[2025-10-22 23:43:22.981+08:00] [C] [238131] terminate called with an active exception, type: N2kj13ExceptionImplE, what: capnp/dynamic.c++:1710: failed: expected value >= 0 [-1 >= 0]; Value out-of-range for requested type.; value = -1

This PR fixes it.

Test plan

  • New test_uint64 tests fail without the fix (verified by reverting and rebuilding)
  • All 3 new tests pass with the fix applied
  • Full capnproto test suite (48 tests) passes with no regressions

The Writer was casting all integral types to std::int64_t, which causes
uint64_t values like UINT64_MAX to overflow to -1 and trigger a Cap'n
Proto assertion: "expected value >= 0". This adds a std::is_unsigned
branch (before the std::is_integral fallback) in add_value_to_array,
add_value_to_object, and add_value_to_union so unsigned types are cast
to std::uint64_t instead.

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 serialization logic that caused unsigned integer types to be mishandled, resulting in data corruption or runtime errors. The changes introduce proper type-aware casting for unsigned integers and are accompanied by comprehensive new tests to prevent future regressions and ensure data integrity.

Highlights

  • Fix Cap'n Proto Serialization: Resolved an issue where the Cap'n Proto Writer incorrectly cast unsigned integral types to std::int64_t, leading to overflow and assertion failures for large unsigned values.
  • Correct Type Handling: Implemented a std::is_unsigned branch in add_value_to_array, add_value_to_object, and add_value_to_union to ensure unsigned types are correctly cast to std::uint64_t during serialization.
  • New Test Coverage: Added test_uint64.cpp to specifically test the serialization of uint64_t, uint32_t, uint16_t, and uint8_t types, covering both maximum and zero values.
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

This pull request correctly fixes a serialization bug for unsigned integer types in the Cap'n Proto writer by introducing a specific handling path for them. The changes are applied consistently across array, object, and union writing functions. The new tests adequately cover the fix. My review includes a suggestion to remove a redundant test case and a recommendation to refactor duplicated code for better maintainability in the future.

@lixin-wei
Copy link
Contributor Author

The failed checks seems not related to my change.

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